How to plot correct colors in R maps library

别来无恙 提交于 2019-12-10 21:56:15

问题


I am trying to plot specific colors for specific countries using R maps library. I can fill in the colors but they are not correctly associated with their respective countries. I wonder if someone could have a clue why?

My data frame is «filld» and has 3 columns: the first is the countries names, the second is just some numeric data, and the 3rd is the color:

              countries toplot     color
1             Argentina     -1       red
2               Armenia     -1       red
3             Australia     -1       red
4               Bahrain     -1       red
5              Botswana     -1       red
6               Belgium     -1       red
7              Bulgaria     -1       red
8                Canada     -1       red
9                 Chile     -1       red
10               Taiwan     -1       red
11              Croatia     -1       red
12       Czech Republic     -1       red
13     UK:Great Britain     -1       red
14                Egypt     -1       red
15              Denmark     -1       red
16              Finland      0    yellow
17               France      0    yellow
18              Georgia      0    yellow
19              Germany      0    yellow
20      China:Hong Kong      0    yellow
21              Hungary      0    yellow
22            Indonesia      0    yellow
23                 Iran      0    yellow
24              Ireland      0    yellow
25               Israel      0    yellow
26                Italy      0    yellow
27                Japan      0    yellow
28               Jordan      0    yellow
29           Kazakhstan      1 darkgreen
30                Korea      1 darkgreen
31               Kuwait      1 darkgreen
32              Lebanon      1 darkgreen
33            Lithuania      1 darkgreen
34             Malaysia      1 darkgreen
35                Malta      1 darkgreen
36              Morocco      1 darkgreen
37          Netherlands      1 darkgreen
38          New Zealand      1 darkgreen
39  UK:Northern Ireland      1 darkgreen
40               Norway      1 darkgreen
41                 Oman      1 darkgreen
42            Palestine      1 darkgreen
43               Poland      1 darkgreen
44             Portugal      1 darkgreen
45                Qatar      1 darkgreen
46               Russia      1 darkgreen
47         Saudi Arabia      0    yellow
48               Serbia      0    yellow
49            Singapore      0    yellow
50      Slovak Republic      0    yellow
51             Slovenia     -1       red
52         South Africa     -1       red
53                Spain     -1       red
54               Sweden     -1       red
55             Thailand      1 darkgreen
56               Turkey      1 darkgreen
57 United Arab Emirates      0    yellow
58                  USA      1 darkgreen

This is the code I am using:

library(maps)       # Provides functions that let us plot the maps
library(mapdata)    # Contains the hi-resolution points that mark out the countries.

map('world', filld$countries, fill=T, border="darkgray", col=filld$color)
map('world', col="darkgray", add=T)

But this is the colors I am getting: Australia should be filled in red, but is green; Spain should be filled in red, but is yellow; France should be filled yellow but it darkgreen;etc... Some countries are ok though, e.g. the USA should be and is darkgreen.

Any comments will be appreciated. Thanks!


回答1:


I'm not entirely sure what creates the problem, but plotting the world first and then filling by color does the trick.

map('world', col='darkgray')
for (color in unique(filld$color)) {
    map('world', regions=filld$countries[which(filld$color==color)], fill=T, border="darkgray", col=color,add=T)
}




回答2:


The cause of the original problem is that

map('world', filld$countries, fill=T, border="darkgray", col=filld$color)

does not return a set of polygons with exactly the same length as the colour vector. If a country consists of several polygons (e.g. islands), these are all separate. Japan, to give just one example that appears in your data, consists of 34 polygons:

z <- map('world',region='japan')
z$names

So the colours are no longer correctly aligned.

You could simply add the option exact=TRUE, but then only the main polygon of each country would be coloured (the one which fits the name exactly), and that isn't even defined for all countries.

For choropleths with the 'maps' package, your best solution is to use match.map(), which gives consecutive numbers to the chosen regions (all polygons):

sel_c <- match.map("world",filld$countries)
map('world',col=filld$col[sel_c],border="darkgrey",fill=TRUE)



回答3:


Following @Richard suggestion:

library(maps) 
library(ggplot2)

map <- map_data("world")
map <- subset(map, region!="Antarctica")
map <- spTransform(map, CRS("+proj=robin")) #Not working, don't know why...
TimssCountries<-ggplot() + 
  geom_polygon(data = map, aes(x=long, y = lat, group = group), fill = NA, colour="darkgray", size=0.25)+
  geom_map(data=filld,map=map,aes(map_id=country, x=lon, y=lat), fill = "filld$color", colour = "gray") +
  coord_equal()
TimssCountries

But, I don't know how to add the color legend with ggplot, so to have a similar effect to this other map:

Thanks!...




回答4:


Just for referece if someone is looking for a similar solution:scale_fill_identity plots the colors in the correct order. The full code is:

TimssDif<-TimssCountries +
  geom_map(data = data, map = map, aes(map_id = country, fill = color), colour="darkgray") +
  theme(legend.title = element_blank()) + # omit plot title saying 'color'
  scale_fill_identity("Title legend", labels = c("Below mean", "At mean", "Above mean"), breaks = plotclr, guide = "legend")
TimssDif + theme(legend.position = "bottom")



来源:https://stackoverflow.com/questions/38901964/how-to-plot-correct-colors-in-r-maps-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!