简体   繁体   English

一张ggplot2图上有两个单独的颜色渐变色标

[英]Two seperate color gradient color scales on one ggplot2 map

I am trying to plot a map that has two different color scales on it for different countries. 我正在尝试绘制一张针对不同国家/地区具有两种不同色标的地图。

Example of my data: 我的数据示例:

>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

I would like there to be one gradient (blues) showing the number of responses for northamerica and another one (reds) showing the number of responses for europe on the same world map. 我想那里是一个梯度(蓝色)表示的用于响应的数量northamerica和示出的用于响应的数量另一个(红色) europe相同的世界地图上。

I have tried subsetting the data and doing two different geom_map lines but I get Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale. 我尝试对数据进行子集处理并执行两条不同的geom_map行,但是我已经获得Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale. 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

I just get the last color scale coded instead of both: 我只是得到了最后一个色标编码,而不是两者都编码了:

I can get the two sets of data to be different colors using this code (below) but I can not figure out how to get it to be scaled by the number of responses in this case. 使用下面的代码,我可以使两组数据具有不同的颜色,但是在这种情况下,我无法弄清楚如何根据响应的数量对数据进行缩放。

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

Different colors for the two regions but not scaled by responses 两个区域的颜色不同,但未根据响应进行缩放

I have tried other variations of code that would take a while to include here and some of them I would be embarrassed to show since I reached a point where I was just trying anything and everything. 我尝试了其他代码变体,可能需要一段时间才能包含在此处,其中有些自从我到达要尝试任何事物的程度时就不好意思展示了。 If anyone has any suggestions that would be wonderful. 如果有人有任何建议,那就太好了。 I would like to avoid making two maps and then putting them on top of each other but if it comes to it I will do that. 我想避免制作两张地图,然后将它们放在彼此的顶部,但是如果涉及到这一点,我会这样做。

Thank you in advance! 先感谢您!

I think this will get you close to what you want. 我认为这将使您接近所需的内容。

# 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"

The deepest color of each is constant from what you had, but I'm scaling the colors by changing their transparency instead of the lowest color, so I can't specify the lightest color. 每种颜色的最深颜色与您拥有的颜色是恒定的,但是我正在通过更改其透明度而不是最低颜色来缩放颜色,因此我无法指定最浅的颜色。 I can adjust how transparent it is at the bottom - and you can play with that. 我可以调整底部的透明度-您可以使用它。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM