【问题标题】:Two seperate color gradient color scales on one ggplot2 map一个 ggplot2 地图上的两个单独的颜色渐变色标
【发布时间】:2018-04-24 03:36:10
【问题描述】:

我正在尝试为不同的国家绘制一张有两种不同颜色比例的地图。

我的数据示例:

>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

两个区域的颜色不同,但未按响应缩放

我尝试了其他代码变体,这些变体需要一段时间才能包含在此处,其中一些我会不好意思展示,因为我达到了我只是尝试任何事情的地步。如果有人有任何建议,那就太好了。我想避免制作两张地图,然后将它们放在一起,但如果涉及到它,我会这样做。

提前谢谢你!

【问题讨论】:

  • 嗨-我有一个想法给你,但我需要一个简单的、可重复的数据集来测试它。我找到了maptools 包来获取wrld_simpl,但是当我尝试运行您的强化功能时出现错误。我对你的想法涉及调整 alpha。如果你可以让它自己工作,那就太好了——如果不能,你需要做一个可重现的例子。
  • 这让我觉得ggplot2 会阻止它,因为它会导致难以解释并可能产生误导的可视化效果。它可能会给人一种印象,即价值观没有明显不同。您可以生成 2 个面板,分别放大到欧洲和北美以突出显示这些区域,但即便如此,我也不会使用不同的颜色渐变。
  • 类似问题的答案中的几个选项:Make your own colors,然后使用“身份”比例或通过按组着色来欺骗事物and then using alpha
  • @MelissaKey 抱歉。我是新来的。我刚刚编辑了我的问题以包含软件包。你必须加载ggplot2 来强化地图。我一直在搞乱 alpha 但我似乎无法得到任何工作。 @neilfws 好的,如果ggplot2 不让我这样做,您还有其他建议吗? @aosmith - 我只是想分配我的颜色并使用“身份”,但似乎无法正常工作。

标签: r ggplot2 colors


【解决方案1】:

我认为这会让你接近你想要的。

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

每种颜色的最深颜色与您所拥有的颜色相同,但我通过更改其透明度而不是最低颜色来缩放颜色,因此我无法指定最亮的颜色。我可以调整它在底部的透明度 - 你可以玩它。

【讨论】:

  • 谢谢@MelissaKey。这是一种巧妙的方法。我将不得不更多地摆弄它,尽管在我的真实数据中,加拿大比任何其他国家都多约 1000 条回复,所以它目前的加拿大是黑暗的,然后所有其他国家都是最苍白的颜色。但谢谢你让我走到这一步!不幸的是,我没有足够的声望点来点击向上箭头 - 抱歉。
  • 使用对数刻度来解决这个问题
猜你喜欢
  • 1970-01-01
  • 2020-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多