Labelling a fortified GADM map plotted with ggplot and geom_map
Thanks Roger, I have been able to gererate a rudimentary map with my data, thanks to your directions. Much appreciated. This is where I am at so far:
library(tmap)
library(tmaptools)
library(viridis)
library(sf)
# read in the map shapefile as a "simple features" (sf)
can_map <- read_shape("~/gdrive_kcj/r-projects/kcj_kids_map/CAN_adm1.shp", as.sf = TRUE)
# create subsets of the kcj data if needed, for example by year
kids_data_2017 <- subset(kids_data, year == 2017) # data for the year 2017
kids_data_2018 <- subset(kids_data, year == 2018) # data for the year 2018
# extract the needed data
kids_province_data <- data.table::data.table(kids_data_2017$Province_Territory__c, kids_data_2017$Number_of_kids__c)
names(kids_province_data)[1] <- "NAME_1"
names(kids_province_data)[2] <- "Number"
# sum the data by province
kids_province_sums <- aggregate(.~NAME_1, data = kids_province_data, sum)
# join the data to the map
by_province <- merge(can_map, kids_province_sums, by = "NAME_1")
# use tmap package to draw map
kids_map <- tm_shape(by_province) +
? tm_polygons("Number", ?title = "Children Reached", subtitle = "2017") +
? tm_text("Number") +
? tm_style_natural() +
? tm_legend(text.size=1,
? ? ? ? ? ? title.size=1.2,
? ? ? ? ? ? position = c("left","top"),
? ? ? ? ? ? bg.color = "white",
? ? ? ? ? ? bg.alpha=.2,
? ? ? ? ? ? frame="gray50",
? ? ? ? ? ? height=.6)
I will continue to explore the tmap documentation to fine tune it! Cheers
On Mar 19, 2018, 1:28 PM -0400, Roger Bivand <Roger.Bivand at nhh.no>, wrote:
Please see the vignettes of the sf package, usually a careful merge() works, and you cease having to fortify. Roger Bivand Norwegian School of Economics Bergen, Norway Fra: Kenneth Dyson Sendt: mandag 19. mars, 18.26 Emne: Re: [R-sig-Geo]? Labelling a fortified GADM map plotted with ggplot and geom_map Kopi: r-sig-geo at r-project.org Pardon, mistakenly hit reply instead of reply-all. Thanks for taking interest I can?t figure out how to merge my collected data into the shapefile without first changing it to a df. Is there something simple I am missing? I will read up on tmap in greater detail asap, but if there is a simple solution to getting the data into the shape file and plotting with data labels please let me know. Cheers On Mar 19, 2018, 12:40 PM -0400, Roger Bivand , wrote: > On Mon, 19 Mar 2018, Kenneth Dyson wrote: > > > Reffered here from the?R-Help mailing list: > > > > I am having trouble getting data labels to display over the provinces in > > a GADM map of Canada. Specifically, I need the variable "Number" from > > the data set "by_province", grouped by "region", to appear on the > > corresponding regions of the map. > > You are making life unnecessarily difficult. Use the tmap package instead, > see for example: > > https://www.johnmackintosh.com/2017-09-01-easy-maps-with-tmap/ > > using the sf representation and forgetting ggplot2 (the development > version supports geom_sf, but hasn't been released). > > Roger > > > > > The data set "by_province" looks like this: > > > > long? ? ? lat? ? ? ? ? order? hole piece ? ? ? ? ? ? ? ? ? region ? ? ? ? ? ? ? ?group ? ? Number > > -110.37074 60.00006 ? ? ? 1 FALSE ? ? 1 ? ? ? ? ? ?Alberta? ? ? ? ? ? Alberta.1? ? 132 > > -110.36250 60.00006 ? ? ? 2 FALSE ? ? 1 ? ? ? ? ? ?Alberta? ? ? ? ? ??Alberta.1? ? 132 > > -110.35103 60.00006 ? ? ? 3 FALSE ? ? 1 ? ? ? ? ? ?Alberta? ? ? ? ? ? Alberta.1? ? 132 > > > > and the data set "tract" is the map data without the "Number" variable. > > > > I looked into working this out from by researching the geom_map function here: http://ggplot2.tidyverse.org/reference/geom_map.html > > > > my code looks like this: > > # get the raw map data > > can_map > tract > > > # create subsets of the kcj data if needed, for example by year > > kids_data_2017 > kids_data_2018 > > > # extract the needed data > > kids_province_data > names(kids_province_data)[1] > names(kids_province_data)[2] > > > # sum the data by province > > kids_province_sums > > > # join the data to the map > > names(tract)[6] > names(kids_province_sums)[1] > by_province > > > # create the data map > > kids_map > ?? ? ? ? ? geom_map(aes(fill = Number), ? ? ? ? ? ? ? ? ? ? ? ? ? ? #generates aestheticsa for the plot > > ? ? ? ? ? ? ? ? ? ? map = tract,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #takes the data from tract > > ? ? ? ? ? ? ? ? ? ? color = "#ffffff",? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #makes the color of the borders between regions white > > ? ? ? ? ? ? ? ? ? ? size = 0.15) +? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #sets the thickness of the boarder lines > > ?? ? ? ? ? coord_map("polyconic") + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #sets the coordinates to polyconic (rounded lat and long) > > ?? ? ? ? ? scale_fill_gradient(name = "Children Reached", ? ? ? ? ? #sets the gradient of the value scale: names the scale > > ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? low = grey_2,? ? ? ? ? ? ? ? ? ? ? ? ? ? #color of the low end > > ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? high = orange_1) + ? ? ? ? ? ? ? ? ? ? ? #color of the high end > > ?? ? ? ? ? expand_limits(x = tract$long,? ? ? ? ? ? ? ? ? ? ? ? ? ? #ensure limits include all values for all plots > > ?? ? ? ? ? ? ? ? ? ? ? ? y = tract$lat) + > > ?? ? ? ? ? labs(x = NULL, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #add labels, no x title > > ? ? ? ? ? ? ? ? y = NULL, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #no y title > > ? ? ? ? ? ? ? ? title = "Number of Children Reached by Province", ? ? ? #map title > > ? ? ? ? ? ? ? ? subtitle = "2017") +? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #map subtitle > > ?? ? ? ? ? geom_text(data = by_province,? ? ? ? ? ? ? ? ? ? ? ? ? ? #add a text layer > > ?? ? ? ? ? ? ? ? ? ? aes(long,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #aethetics for the text, x axis > > ?? ? ? ? ? ? ? ? ? ? ? ? lat, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #y axis > > ?? ? ? ? ? ? ? ? ? ? ? ? label = Number,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #the value to display > > ?? ? ? ? ? ? ? ? ? ? ? ? size=3)) + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #size of the text > > ?? ? ? ? ? theme(axis.ticks = element_blank(),? ? ? ? ? ? ? ? ? ? ? #theme of the graph, no axis ticks > > ?? ? ? ? ? ? ? ? axis.text = element_blank(), ? ? ? ? ? ? ? ? ? ? ? ? ? #no axis text > > ?? ? ? ? ? ? ? ? panel.background = element_blank(),? ? ? ? ? ? ? ? ? ? #blank background > > ?? ? ? ? ? ? ? ? panel.border = element_blank())? ? ? ? ? ? ? ? ? ? ? ? #blank border > > > > # save as png > > ggsave(kids_map, file = "kids_map.png", width = 6, height = 4.5, type = "cairo-png?) > > > > > > I have asked this question on stack overflow, and was refered to this answer: > > https://stackoverflow.com/questions/9441436/ggplot-centered-names-on-a-map > > > > The solution there did not fix my problem, though it did get me closer. > > The solution on that post is using a single vector of labels. > > > > My post on stackoverflow has images of my output: > > https://stackoverflow.com/questions/49118323/labelling-a-map-plotted-with-geom-map > > > > [[alternative HTML version deleted]] > > > > _______________________________________________ > > R-sig-Geo mailing list > > R-sig-Geo at r-project.org > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > > > > -- > Roger Bivand > Department of Economics, Norwegian School of Economics, > Helleveien 30, N-5045 Bergen, Norway. > voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no > http://orcid.org/0000-0003-2392-6140 > https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo