[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-sig-geo
Subject:    Re: [R-sig-Geo] Simple choropleth using ggplot2
From:       Bede-Fazekas_Ákos <bfalevlist () gmail ! com>
Date:       2019-01-17 8:23:16
Message-ID: 9d4b0d44-4f90-3db8-363e-152f9574eb31 () gmail ! com
[Download RAW message or body]

Dear Richard,
although I have problem with your mmr.zip (R cannott unzip it), and 
could not try your code, I   suggest to migrate from sp to sf. ggplot can 
easily use Simple Features data.
Use function sf::st_as_sf() to convert your Spatial* data to Simple 
Features.
HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences

2019.01.17. 3:19 keltezéssel, Richard Sherman írta:
> Hello all,
> 
> I am trying to plot a simple choropleth, something I've done a while ago using \
> rworldmap and also (if I recall correctly) ggplot2, but I am failing to draw the \
> map at all and failing (I think) to merge my data properly with the shapefile. \
> Thank you for help with a basic question. I'd like to know what is wrong with what \
> I'm doing here. 
> My R script is
> 
> library(ggplot2)
> library(rgdal)
> library(plyr)
> 
> # get shapefile for world map
> download.file("https://opendata.arcgis.com/datasets/252471276c9941729543be8789e06e12_0.zip", \
> destfile = "countries.zip") 
> # get world bank maternal mortality data
> download.file("http://api.worldbank.org/v2/en/indicator/SH.STA.MMRT?downloadformat=csv", \
> destfile = "mmr.zip") 
> # get csv file with concordance between ISO-2-alpha and ISO-3-alpha country codes
> download.file("https://raw.githubusercontent.com/rsspdx/mmr/master/iso_2_iso_3.csv", \
> destfile = "iso_2_iso_3.csv") 
> # unzip the zipped files
> mmr.files <- unzip("mmr.zip")
> unzip("countries.zip")
> 
> # read in maternal mortality data and fix it up
> mmr.data <- read.csv(mmr.files[2], skip = 3, stringsAsFactors = FALSE)
> mmr.data.name <- mmr.data$Country.Name
> mmr.data.code <- mmr.data$Country.Code
> mmr.data.mmr <- mmr.data$X2015
> mmr.data.df <- as.data.frame(cbind(mmr.data.name, mmr.data.code, mmr.data.mmr))
> names(mmr.data.df) <- c("Country.Name", "Country.Code", "mmr")
> 
> # read in the shapefile
> world.map <- readOGR(dsn=".", layer = "UIA_World_Countries_Boundaries")
> 
> # --------- possibly I should be doing this ------------
> #
> # world.map@data$id <- rownames(world.map@data)
> # world.map.df <- fortify(world.map)
> #
> # -------------------------------------------------------
> 
> #------or perhaps I need to merge the data into a data slot of the shapefile
> #------but I can't recall (or never knew?) how to do that
> 
> # get ISO2 country codes
> iso_2_iso_3 <- read.csv("iso_2_iso_3.csv")
> 
> # ISO2 in this file is called ISO in the shapefile, create ISO variable
> # then merge into mmr.data
> iso_2_iso_3$ISO <- iso_2_iso_3$ISO2
> mmr.data.df <- merge(iso_2_iso_3, mmr.data.df, by.x="ISO3", by.y="Country.Code")
> 
> # merge maternal mortality data into shapefile
> mmr <- merge(world.map, mmr.data.df, by = "ISO")
> mmr <- fortify(mmr)
> str(mmr)
> 
> # ---------create a map, not working
> map <- ggplot(data = mmr, aes(x = long, y = lat, group = group))
> 
> # ---------look at the map, obviously not working
> map + geom_polygon(fill = mmr$mmr)
> 
> 
> ---
> Richard Sherman
> rss.pdx@gmail.com
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic