Last active
July 28, 2018 09:37
-
-
Save cavedave/027f9dd66bc26072cf10c3ffcc808031 to your computer and use it in GitHub Desktop.
Make a map of ireland with counties coloured. Example image https://imgur.com/a/JYe03Uk A lot of the code below comes from http://rstudio-pubs-static.s3.amazonaws.com/213111_0ef67ba827664a45a1e79f1c6d531e2c.html#/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load up area shape file: | |
#gotten from http://census.cso.ie/censusasp/saps/boundaries/ED_SA%20Disclaimer1.htm | |
# load required libraries | |
library(rgeos) | |
library(maptools) | |
# read the shape file | |
spdf <- readShapePoly( | |
"Census2011_Admin_Counties_generalised20m.shp") | |
# make it ggplot-friendly | |
spdf@data$id <- rownames(spdf@data) | |
spdf.points <- fortify(spdf, region="id") | |
counties <- inner_join(spdf.points, spdf@data, by="id") | |
#test it can draw ireland | |
# plot it | |
ggplot(counties) + geom_polygon(colour="black", fill=NA, aes(x=long, y=lat, group=group)) + coord_fixed() | |
# define a base map layer | |
housingplot_baselayer <- ggplot(counties) + | |
aes(long, lat, group=group) + | |
geom_polygon(colour="grey") | |
# plot a data column just to see it works | |
housingplot_baselayer + aes(fill=`TOTAL2011`) | |
#create a column Type and class counties based | |
counties <- counties %>% | |
mutate(Type = case_when(COUNTYNAME== "Dublin City" ~ "1", | |
#COUNTYNAME == "Fingal" ~ "1", | |
#COUNTYNAME == "D\xfan Laoghaire-Rathdown" ~ "1", | |
COUNTYNAME == "South Dublin" ~ "1", | |
#COUNTYNAME == "Kildare County" ~ "1", | |
COUNTYNAME == "Carlow County" ~ "2", | |
#COUNTYNAME == "Wicklow County" ~ "2", | |
#COUNTYNAME == "Wexford County" ~ "2", | |
COUNTYNAME == "Kilkenny County" ~ "2", | |
COUNTYNAME == "Laois County" ~ "2", | |
COUNTYNAME == "Westmeath County" ~ "2", | |
COUNTYNAME == "Meath County" ~ "2", | |
COUNTYNAME == "Leitrim County" ~ "2", | |
#COUNTYNAME == "Louth County" ~ "2", | |
COUNTYNAME == "Sligo County" ~ "2", | |
COUNTYNAME == "Monaghan County" ~ "2", | |
COUNTYNAME == "Cavan County" ~ "2", | |
COUNTYNAME == "Longford County" ~ "2", | |
COUNTYNAME == "Offaly County" ~ "2", | |
COUNTYNAME == "Roscommon County" ~ "2", | |
# COUNTYNAME == "Waterford City" ~ "2", | |
COUNTYNAME == "Waterford County" ~ "2", | |
COUNTYNAME == "Kilkenny County" ~ "2", | |
#COUNTYNAME == "North Tipperary" ~ "2", | |
#COUNTYNAME == "South Tipperary" ~ "2", | |
TRUE ~ "0")) | |
# line below lets you look for how to spell county name | |
#dplyr::filter(counties, grepl("Waterford",COUNTYNAME)) | |
#the actual graph | |
ggplot(counties) + geom_polygon(colour="black", aes(x=long, y=lat, group=group, fill=Type))+theme_nothing(legend = TRUE) + labs(title = "Purple and Orange have \n the same number of people",caption = "@iamreddave")+theme(plot.title = element_text(hjust = 0.5))+ scale_fill_manual(values=c("#008000", "#d429ff", "#ffa500")) | |
ggsave("IrelandSame.png", width = 5, height = 6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment