在 R 中将 plot_usmap 与颜色和 geom_scatterpie 一起使用

问题描述 投票:0回答:1

我想制作一张地图,每个州都有颜色代表一个变量。同时,我想为每个州添加一个饼图,同时保持上一步的颜色。 到目前为止,我可以用 plot_usmap 制作彩色地图enter image description here,地图上的饼图单独使用 geom_scatterpieenter image description here。 .我想知道我是否可以将它们组合成一张地图

我试图将这两个功能与'+'结合起来,但是没有用

r ggplot2 pie-chart usmap scatterpie
1个回答
0
投票

当然可以。但是你必须知道,默认情况下我们只能有一个

fill
尺度,所以你必须注意映射到
fill
美学上的值类型(连续与离散),用于
plot_usmap
 geom_scatterpie
部分。规避这种情况的一种选择是使用
ggnewscale
包,它允许多个比例以获得与我在下面的示例中所做的相同的美感。

根据我在 this post 上的回答和

plot_usmap
.

中的默认示例使用一些虚假示例数据
library(usmap)
library(ggplot2)
library(scatterpie)
library(ggnewscale)

data <- data.frame(region= c("AL", "AR", "AZ", "CA", "IA"), 
                   gas= c(25, 45, 45, 60, 75),
                   coal= c(45, 50, 45, 20, 15), 
                   wind= c(30, 5, 10, 20, 10), 
                   solar= c(10, 10, 10, 10, 10))

states <- us_map("states")

centroids <- usmapdata::centroid_labels("states")[c("x", "y", "abbr")]

data <- merge(data, centroids, by.x = "region", by.y = "abbr", all.x = TRUE)

plot_usmap(data = statepop, values = "pop_2015") +
  scale_fill_distiller(palette = "BuGn") +
  ggnewscale::new_scale_fill() +
  geom_scatterpie(aes(x, y, group = region),
                  data = data, cols = c("gas", "coal", "wind", "solar")
  ) +
  geom_text(aes(x, y, label = region),
            data = data, vjust = 1, nudge_y = -100000
  ) +
  theme(legend.position = "left")

© www.soinside.com 2019 - 2024. All rights reserved.