Alex Karvouniaris
Alex Karvouniaris

Reputation: 343

How to add a country to country.map-country.region for a choropleth map using choroplethr- choroplethrMaps-

I have already made a map using choroplethr and choroplethrMaps packages . My problem is that in order to plot a country , this country must be contained in the country.regions and country.map data frames that are included in the choroplethrMaps package. So lets say this is my map

install.packages("mapproj");library(mapproj)
install.packages("choroplethr")
install.packages("choroplethrMaps")
library(choroplethr);library(choroplethrMaps);library(ggplot2)
data(country.map)
data(country.regions)
a<-c(4.1,2.5,0.4,6.4,1.4,1.8,3.8,1.3,2.3,8.4,5.2,1.9,0.8,1.5,2.1,1.2,3.8,1.4,3.1,0.8,4.0,1.3,4.8,2.6,2.8,2.3,3.1,2.5,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)
target<-c("austria","belgium","bulgaria","switzerland","cyprus","czech republic","denmark",
          "estonia","spain","finland","france","greece","croatia","hungary","ireland","italy",
          "lithuania","luxembourg","latvia","norway","poland","portugal","romania","sweden",
          "slovenia","slovakia","turkey","united kingdom","russia","belarus","germany","ukraine","iceland"
          ,"netherlands","bosnia and herzegovina","albania","montenegro","macedonia","moldova","kosovo")
datas<-data.frame(region=target,value=a)
datas$region<-as.character(datas$region)
gg <- country_choropleth(datas,num_colors=1,zoom=target)
gg <- gg + xlim(-25.266001, 71.869301)
gg <- gg + ylim(34.536311, 70.008797)
gg <- gg + coord_map("lambert", lat0=27.636311, lat1=81.008797)
gg<-gg+scale_fill_continuous(name="%",low="yellow", high="orange", na.value="grey")
gg<-gg+theme(plot.background=element_rect(fill="dodgerblue",colour="black"),legend.position="bottom",legend.background = element_rect(),legend.text = element_text(size = 13, colour = "black")) 
gg

enter image description here

Everything goes fine because every region that i have in my data frame is contained with the same name in the country.regions and country.map .

My problem is that i want to add malta and serbia regions to my map , for which i have data for , and i cant do it because they are not contained by the developers in the country.regions and country.map data frames .I tried to add the names of the countries to country.regions and their latitudes and longtitudes in country.map (using the geocode("malta") function of the maps package) but it does not work that way

Upvotes: 1

Views: 1541

Answers (2)

giocomai
giocomai

Reputation: 3518

For the records, choroplethrMaps does include Serbia, but for some reason you need to call it "republic of serbia", instead than just "serbia".

Upvotes: 1

Ari
Ari

Reputation: 1972

Thank you for using choroplethr.

I think that you will find it difficult to simply add a country to ?country.map. The geocode function that you refer to simply return a single longitude and latitude point. But to draw the country, you need points to represent the entire border.

However, I wrote choroplethr to handle cases like this, where people want to use the choroplethr interface for their own map. Here is how I recommend you get started.

  1. Read the wiki page Mapmaking for R programmers
  2. Read the vignette Creating Your Own Maps
  3. If you have further questions, you might want to post on the choroplethr google group

Essentially, I think that you want to find a shapefile that meets your needs and learn QGIS well enough to open up the shapefile and poke around in it. After that it should be just a few lines of R code and (hopefully) some simple debugging to have choroplethr "just work" for you.

I'm sorry that I can't provide more specific advice. I just don't know where to go for European shapefiles. But if you find that out, or if someone else posts it, I will add it to my list.

Upvotes: 1

Related Questions