代码之家  ›  专栏  ›  技术社区  ›  KTietjen

一张ggplot2地图上的两个独立颜色渐变色比例

  •  2
  • KTietjen  · 技术社区  · 7 年前

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

    我的数据示例:

    >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
    

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

    我尝试了其他的代码变体,这些变体需要一段时间才能包含在这里,其中一些我会很难为情地展示出来,因为我已经到了尝试任何事情和一切的地步。如果有人有什么建议那就太好了。我希望避免制作两张地图,然后把它们放在彼此的顶部,但如果真的这样,我会这样做。

    提前感谢您!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Melissa Key    7 年前

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

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

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

    enter image description here