knitr::opts_chunk$set(echo = TRUE)

library(tidyverse)
library(sf)
library(ggplot2)
library(ggiraph)

Create Base Maps

# I got the Ireland shape file from https://brendanjodowd.github.io/map_guide/#Introduction. 

ireland_map <- st_read("https://raw.githubusercontent.com/brendanjodowd/maps/main/lea_166.geojson")
## Reading layer `lea_166' from data source 
##   `https://raw.githubusercontent.com/brendanjodowd/maps/main/lea_166.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 167 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -10.66284 ymin: 51.41991 xmax: -5.432917 ymax: 55.44644
## Geodetic CRS:  WGS 84
ggplot(ireland_map) + geom_sf()

2 blank maps of Ireland, one with all of the voting districts outlined, and the other with just the counties outlined

county_map <- ireland_map %>% 
  group_by(COUNTY) %>% 
  summarise(geometry = st_union(geometry) , AREA=sum(AREA))

ggplot(county_map) + geom_sf()

2 blank maps of Ireland, one with all of the voting districts outlined, and the other with just the counties outlined

Create Datatset

# I got the sheep dataset from https://data.gov.ie/dataset/dafm-national-sheep-census-2020. 

sheep <- read.csv('sheep_census_2021_2.csv')

sheep <- sheep %>%
  mutate(COUNTY = case_when(COUNTY == 'CARLOW' ~ 'Carlow',
                            COUNTY == 'CAVAN' ~ 'Cavan',
                            COUNTY == 'CLARE' ~ 'Clare',
                            COUNTY == 'CORK' ~ 'Cork',
                            COUNTY == 'DONEGAL' ~ 'Donegal',
                            COUNTY == 'DUBLIN' ~ 'Dublin',
                            COUNTY == 'GALWAY' ~ 'Galway',
                            COUNTY == 'KERRY' ~ 'Kerry',
                            COUNTY == 'KILDARE' ~ 'Kildare',
                            COUNTY == 'KILKENNY' ~ 'Kilkenny',
                            COUNTY == 'LAOIS' ~ 'Laois',
                            COUNTY == 'LEITRIM' ~ 'Leitrim',
                            COUNTY == 'LIMERICK' ~ 'Limerick',
                            COUNTY == 'LONGFORD' ~ 'Longford',
                            COUNTY == 'LOUTH' ~ 'Louth',
                            COUNTY == 'MAYO' ~ 'Mayo',
                            COUNTY == 'MEATH' ~ 'Meath',
                            COUNTY == 'MONAGHAN' ~ 'Monaghan',
                            COUNTY == 'OFFALY' ~ 'Offaly',
                            COUNTY == 'ROSCOMMON' ~ 'Roscommon',
                            COUNTY == 'SLIGO' ~ 'Sligo',
                            COUNTY == 'TIPPERARY' ~ 'Tipperary',
                            COUNTY == 'WATERFORD' ~ 'Waterford',
                            COUNTY == 'WESTMEATH' ~ 'Westmeath',
                            COUNTY == 'WEXFORD' ~ 'Wexford',
                            COUNTY == 'WICKLOW' ~ 'Wicklow'))
county_map_final <- left_join(county_map, sheep, by = c("COUNTY" = "COUNTY"))

Making Choropleth Map

ggplot(county_map_final) + 
  geom_sf(aes(fill= TOTAL_SHEEP_CALC))

choropleth map of the republic of ireland with each county filled with a shade of blue that indicates the number of sheep in that county

Adding Details to Choropleth Map

#get rid of scientific notation
options(scipen=999)
ggplot(county_map_final) + 
  geom_sf(aes(fill= TOTAL_SHEEP_CALC))

5 choropleth maps of the republic of ireland with each county filled with a color that indicates the number of sheep in that county, first plot has blue fill and the numbers in the legend no longer have scientific notation, the second plot has now added a title, subtitle, and caption to the map, the third plot has now changed the fill color to be green instead of blue and added a title to the legend, the fourth plot is the same as the third but without a grey backgound, fifth plot is the same as the previous but when you hover your mouse over each county, text pops up that tells you the name of the county and the exact number of sheep in that county.

#add labels
ggplot(county_map_final) + 
  geom_sf(aes(fill= TOTAL_SHEEP_CALC))+
  labs(title = 'Number of Sheep Per County', subtitle= 'Republic of Ireland', caption = 'Visualization by Your Name Here')

5 choropleth maps of the republic of ireland with each county filled with a color that indicates the number of sheep in that county, first plot has blue fill and the numbers in the legend no longer have scientific notation, the second plot has now added a title, subtitle, and caption to the map, the third plot has now changed the fill color to be green instead of blue and added a title to the legend, the fourth plot is the same as the third but without a grey backgound, fifth plot is the same as the previous but when you hover your mouse over each county, text pops up that tells you the name of the county and the exact number of sheep in that county.

#change legend
ggplot(county_map_final) + 
  geom_sf(aes(fill= TOTAL_SHEEP_CALC))+
  labs(title = 'Number of Sheep Per County', subtitle= 'Republic of Ireland', caption = 'Visualization by Your Name Here')+
    scale_fill_gradientn(name= 'Number of Sheep', colours = c("white", "limegreen", "darkgreen"))

5 choropleth maps of the republic of ireland with each county filled with a color that indicates the number of sheep in that county, first plot has blue fill and the numbers in the legend no longer have scientific notation, the second plot has now added a title, subtitle, and caption to the map, the third plot has now changed the fill color to be green instead of blue and added a title to the legend, the fourth plot is the same as the third but without a grey backgound, fifth plot is the same as the previous but when you hover your mouse over each county, text pops up that tells you the name of the county and the exact number of sheep in that county.

#add theme
ggplot(county_map_final) + 
  geom_sf(aes(fill= TOTAL_SHEEP_CALC))+
  labs(title = 'Number of Sheep Per County', subtitle= 'Republic of Ireland', caption = 'Visualization by Your Name Here')+
    scale_fill_gradientn(name= 'Number of Sheep', colours = c("white", "limegreen", "darkgreen"))+
  theme_minimal()

5 choropleth maps of the republic of ireland with each county filled with a color that indicates the number of sheep in that county, first plot has blue fill and the numbers in the legend no longer have scientific notation, the second plot has now added a title, subtitle, and caption to the map, the third plot has now changed the fill color to be green instead of blue and added a title to the legend, the fourth plot is the same as the third but without a grey backgound, fifth plot is the same as the previous but when you hover your mouse over each county, text pops up that tells you the name of the county and the exact number of sheep in that county.

#make it interactive
interactivemap <- ggplot(county_map_final) + 
  geom_sf_interactive(aes(fill= TOTAL_SHEEP_CALC, tooltip = c(paste0(COUNTY,"<br>", TOTAL_SHEEP_CALC))))+
  labs(title = 'Number of Sheep Per County', subtitle= 'Republic of Ireland', caption = 'Visualization by Julia Ross')+
    scale_fill_gradientn(name= 'Number of Sheep', colours = c("white", "limegreen", "darkgreen"))+
  theme_minimal()

girafe(ggobj = interactivemap)