我正在尝试为不同的国家/地区绘制一张有两种不同色阶的地图。
我的数据示例:
>df
country responses area
1 Canada 150 northamerica
2 United States 70 northamerica
3 Mexico 65 northamerica
4 Germany 120 europe
5 France 55 europe
6 Spain 71 europe
我希望有一个渐变(蓝调)显示northamerica
和另一个(红色)的回复数量,显示同一世界地图上europe
的回复数量。
我尝试过对数据进行子集化并执行两个不同的geom_map
行,但我得到Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.
na <- df[df$area=="northamerica",]
euro <- df[df$area=="europe",]
library(maptools)
library(ggplot2)
data(wrld_simpl)
wrld_simpl@data$id <- wrld_simpl@data$NAME
wrld <- fortify(wrld_simpl, region="id")
wrld <- subset(wrld, id != "Antarctica")
gg <- ggplot()
gg <- gg + geom_map(data=wrld, map=wrld, aes(map_id=id, x=long, y=lat), fill="white", color="#7f7f7f", size=0.25)
gg <- gg + geom_map(data=na, map=wrld, aes(map_id=country, fill=responses), color="white", size=0.25)
gg <- gg + scale_fill_continuous(high = "#132B43", low = "#56B1F7", name="Number of\nresponses\nNorth America")
gg <- gg + geom_map(data=euro, map=wrld, aes(map_id=country, fill=responses), color="white", size=0.25)
gg <- gg + scale_fill_continuous(high = "#67000d", low = "#fcbba1", name="Number of\nresponses\nEurope")
gg <- gg + coord_map()
gg <- gg + labs(x="", y="")
gg <- gg + theme(plot.background = element_rect(fill = "transparent", colour = NA),
panel.border = element_blank(),
panel.background = element_rect(fill = "transparent", colour = NA),
panel.grid = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = "right")
gg
我只是编码最后一个色标而不是两个:
我可以使用此代码(下面)将两组数据设置为不同的颜色,但我无法弄清楚如何通过这种情况下的响应数量来缩放它。
gg <- ggplot()
gg <- gg + geom_map(data=wrld, map=wrld, aes(map_id=id, x=long, y=lat), fill="white", color="#7f7f7f", size=0.25)
gg <- gg + geom_map(data=na, map=wrld, aes(map_id=country, fill=responses), color="white", size=0.25, fill = "blue")
gg <- gg + geom_map(data=euro, map=wrld, aes(map_id=country, fill=responses), color="white", size=0.25, fill = "red")
gg <- gg + coord_map()
gg <- gg + labs(x="", y="")
gg <- gg + theme(plot.background = element_rect(fill = "transparent", colour = NA),
panel.border = element_blank(),
panel.background = element_rect(fill = "transparent", colour = NA),
panel.grid = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = "right")
gg
两个区域的颜色不同,但没有按响应缩放
我尝试了其他代码的变体,这些代码需要一段时间才能包含在这里,其中一些我会很尴尬地展示,因为我已经达到了我只是尝试任何东西的一切。如果有人有任何建议会很棒。我想避免制作两张地图,然后将它们放在彼此的顶部,但如果涉及到它,我会这样做。
提前谢谢!
答案 0 :(得分:0)
我认为这会让你接近你想要的。
# Other packages needed in order to to run this code
install.packages("rgeos", "mapproj")
ggplot() +
geom_map(data=wrld, map=wrld, aes(map_id=id, x=long, y=lat), fill="white", color="#7f7f7f", size=0.25) +
geom_map(data=df, map=wrld, aes(map_id=country, fill=area, alpha = responses), color="white", size=0.25) +
scale_fill_manual(values = c("#132B43", "#67000d")) +
scale_alpha_continuous(range = c(0.3, 1)) +
coord_map() +
labs(x="", y="") +
theme(plot.background = element_rect(fill = "transparent", colour = NA),
panel.border = element_blank(),
panel.background = element_rect(fill = "transparent", colour = NA),
panel.grid = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = "right"
每种颜色的最深颜色与您所拥有的颜色不同,但我通过改变透明度而不是最低颜色来缩放颜色,因此我无法指定最亮的颜色。我可以调整它底部的透明度 - 你可以玩它。