Fill specific countries with different colour

843 views
Skip to first unread message

Sarah Könemann

unread,
Jun 23, 2016, 4:47:08 AM6/23/16
to rworldmap
Hey guys,

I made a map with rworldmap and right now my code looks like this:

library(rworldmap)

newmap <- getMap(resolution = "high")
plot(newmap, xlim = c(-10,19), ylim c(40,56), col = "gray90", fill = TRUE)

This gives me a certain excerpt of Europe with all countries colored in light gray. Now, I would like to additionally highlight Spain, France, Belgium, Germany, Italy, Austria and Czech Republic (preferably in darker gray) but I absolutely don´t know how to do it. I tried several things I found on the internet but it did not work...

I hope there is an easy way to do it ;-)

Many thanks in advance!

Andy South

unread,
Jun 24, 2016, 8:29:53 AM6/24/16
to Sarah Könemann, rworldmap
Hi Sarah,

You're nearly there. This in reply to a later message you sent me. You were missing a c in the first line, and Name needed to be NAME. Then this should be all you need to start with a 2 grey map.

dF <- data.frame(country = c("spain","france","belgium","germany","austria","italy"), sampled = c(1,1,1,2,2,2))
sPDF <- joinCountryData2Map(dF,joinCode = "NAME", nameJoinColumn = "country", mapResolution = "low")
mapCountryData(sPDF, nameColumnToPlot= "sampled", catMethod = 'categorical', xlim=c(-10,19), ylim=c(40,56), colourPalette=c("gray90","gray30"))

Add extra countries and you can change sampled to e.g. sampled = c("name1","name1","name1","name2","name2","name2")

Help creating a map of Europe is the least I can do on a sad day like today !

Might also be worth looking at the tmap package which is good for doing this type of thing too and a bit newer.

Good luck,
Andy


--
You received this message because you are subscribed to the Google Groups "rworldmap" group.
To post to this group, send email to rwor...@googlegroups.com.
Visit this group at https://groups.google.com/group/rworldmap.
To view this discussion on the web, visit https://groups.google.com/d/msgid/rworldmap/81840b46-1d37-4759-b571-e9295c869f6a%40googlegroups.com.

Sarah Könemann

unread,
Jun 24, 2016, 9:10:08 AM6/24/16
to rworldmap, sarah.k...@t-online.de
Hey Andy,

thank you for your immediate reply.
I corrected my code but unfortunately it does not work. This is my code so far:

library(rworldmap)

svg("Map.svg")

#Create map


newmap <- getMap(resolution = "high")

plot(newmap, xlim=c(-10,19), ylim=c(40,56), col= "#C0C0C0", fill=TRUE)

par(new=TRUE)


newmap <- getMap(resolution = "high")

dF <- data.frame(country = c("spain","france","belgium","germany","austria","italy","czechrepublic), sampled = c(1,1,1,2,2,2,2))

sPDF <- joinCountryData2Map(dF,joinCode = "NAME", nameJoinColumn = "country", mapResolution = "low")

mapCountryData(sPDF, nameColumnToPlot = "sampled", catMethod = "categorical", xlim=c(-10,19), ylim=c(40,56), colourPalette = c("gray90", "gray30"))

dev.off()


The error message is the following:

In par(new = TRUE) : calling par(new=TRUE) with no plot

>
> newmap <- getMap(resolution = "high")
Error: could not find function "getMap"
>
> dF <- data.frame(country = c("spain","france","belgium","germany","austria","italy","czechrepublic"), sampled = c(1,1,1,2,2,2,2))

> sPDF <- joinCountryData2Map(dF,joinCode = "NAME", nameJoinColumn = "country", mapResolution = "low")
Error: could not find function "joinCountryData2Map"
>
> mapCountryData(sPDF, nameColumnToPlot = "sampled", catMethod = "categorical", xlim=c(-10,19), ylim=c(40,56), colourPalette = c("gray90", "gray30"))
Error: could not find function "mapCountryData"


Sarah

Andy South

unread,
Jun 24, 2016, 9:14:47 AM6/24/16
to Sarah Könemann, rworldmap

Hi Sarah,
The error messages show that you don't have rworldmap loaded.

You should only need library(rworldmap) in front of the 3 lines I sent previously. (i.e. you don't need the getMap etc. lines).
Andy

--
You received this message because you are subscribed to the Google Groups "rworldmap" group.
To post to this group, send email to rwor...@googlegroups.com.
Visit this group at https://groups.google.com/group/rworldmap.

Sarah Könemann

unread,
Jun 24, 2016, 9:41:10 AM6/24/16
to rworldmap
Thanks a lot Andy!

Now it is working fine =)

Only one more question (I promise ;-)): the map looks a little contorted, like one of the projection maps. Is it possible to plot a "plain" map?

Thank you and have a nice weekend

Sarah

Andy South

unread,
Jun 24, 2016, 10:23:51 AM6/24/16
to Sarah Könemann, rworldmap
Hi Sarah,
The default map is in lat long. It is possible to re-project the map using rgdal, but to be honest I'd reccommend having a look at tmap which does projections and Europe maps better :


Enjoy your weekend too.
Andy
 

--
You received this message because you are subscribed to the Google Groups "rworldmap" group.
To post to this group, send email to rwor...@googlegroups.com.
Visit this group at https://groups.google.com/group/rworldmap.
Reply all
Reply to author
Forward
0 new messages