如何在 R 地图库中绘制正确的颜色
How to plot correct colors in R maps library
我正在尝试使用 R 地图库为特定国家/地区绘制特定颜色。我可以填写颜色,但它们没有正确地与各自的国家相关联。我想知道是否有人知道为什么?
我的数据框是 «filld»,有 3 列:第一列是国家名称,第二列只是一些数字数据,第三列是颜色:
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
这是我使用的代码:
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)
但这是我得到的颜色:
澳大利亚应该填红色,但是是绿色;西班牙应该是红色的,但是是黄色的;法国应该是黄色的,但它是深绿色的;等等……
不过有些国家还可以,例如美国应该是深绿色。
如有任何意见,我们将不胜感激。谢谢!
我不完全确定是什么导致了这个问题,但是先绘制世界然后用颜色填充就可以了。
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)
}
遵循@Richard 建议:
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
但是,我不知道如何使用 ggplot 添加颜色图例,所以要与其他地图有类似的效果:
谢谢!...
原问题的原因是
map('world', filld$countries, fill=T, border="darkgray", col=filld$color)
不return一组与颜色向量长度完全相同的多边形。如果一个国家/地区由多个多边形(例如岛屿)组成,则这些多边形都是独立的。日本,仅举一个出现在您的数据中的例子,由 34 个多边形组成:
z <- map('world',region='japan')
z$names
因此颜色不再正确对齐。
您可以简单地添加选项 exact=TRUE
,但这样只有每个国家/地区的主要多边形会被着色(与名称完全匹配的多边形),而且甚至没有为所有国家/地区定义。
对于带有 'maps' 包的等值线,您最好的解决方案是使用 match.map()
,它为所选区域(所有多边形)提供连续的数字:
sel_c <- match.map("world",filld$countries)
map('world',col=filld$col[sel_c],border="darkgrey",fill=TRUE)
如果有人正在寻找类似的解决方案,仅供参考:scale_fill_identity
以正确的顺序绘制颜色。完整代码为:
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")
我正在尝试使用 R 地图库为特定国家/地区绘制特定颜色。我可以填写颜色,但它们没有正确地与各自的国家相关联。我想知道是否有人知道为什么?
我的数据框是 «filld»,有 3 列:第一列是国家名称,第二列只是一些数字数据,第三列是颜色:
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
这是我使用的代码:
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)
但这是我得到的颜色:
如有任何意见,我们将不胜感激。谢谢!
我不完全确定是什么导致了这个问题,但是先绘制世界然后用颜色填充就可以了。
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)
}
遵循@Richard 建议:
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
但是,我不知道如何使用 ggplot 添加颜色图例,所以要与其他地图有类似的效果:
谢谢!...
原问题的原因是
map('world', filld$countries, fill=T, border="darkgray", col=filld$color)
不return一组与颜色向量长度完全相同的多边形。如果一个国家/地区由多个多边形(例如岛屿)组成,则这些多边形都是独立的。日本,仅举一个出现在您的数据中的例子,由 34 个多边形组成:
z <- map('world',region='japan')
z$names
因此颜色不再正确对齐。
您可以简单地添加选项 exact=TRUE
,但这样只有每个国家/地区的主要多边形会被着色(与名称完全匹配的多边形),而且甚至没有为所有国家/地区定义。
对于带有 'maps' 包的等值线,您最好的解决方案是使用 match.map()
,它为所选区域(所有多边形)提供连续的数字:
sel_c <- match.map("world",filld$countries)
map('world',col=filld$col[sel_c],border="darkgrey",fill=TRUE)
如果有人正在寻找类似的解决方案,仅供参考:scale_fill_identity
以正确的顺序绘制颜色。完整代码为:
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")