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()

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

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))

Adding Details to Choropleth Map
#get rid of scientific notation
options(scipen=999)
ggplot(county_map_final) +
geom_sf(aes(fill= TOTAL_SHEEP_CALC))

#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')

#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"))

#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()

#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)