如何创建颜色基于一个变量(离散)但渐变基于另一个变量(连续)的贴图

时间:2019-06-26 15:36:45

标签: r ggplot2

我想重新创建流行与苏打地图。我正在使用state库中的maps。这是我正在使用的代码。

states = map_data("state")

states%>%
  full_join(soft_drinks_plot, by = c("region" = "State"))%>%
  ggplot()+
  geom_polygon(aes(x = long, y = lat, group = group, 
fill = max_choice, color = max_choice), color = "white")+
  scale_fill_gradient2(aesthetics = "color")

我的soft_drinks_plot数据框看起来像这样:

State            Pop  Soda  Coke Other Total max_choice per_max
alabama          153   582  2849   665  4249 Coke         0.671
alaska           324   636    60    92  1112 Soda         0.572
arizona          586  2799   437   174  3996 Soda         0.700
arkansas         154   347  1442    80  2023 Coke         0.713
california       925 20119  2892  1941 25877 Soda         0.777

以下是重新创建它的代码:

soft_drinks_plot <- read.table(text =
"State            Pop  Soda  Coke Other Total max_choice per_max
alabama          153   582  2849   665  4249 Coke         0.671
alaska           324   636    60    92  1112 Soda         0.572
arizona          586  2799   437   174  3996 Soda         0.700
arkansas         154   347  1442    80  2023 Coke         0.713
california       925 20119  2892  1941 25877 Soda         0.777",
header = TRUE, stringsAsFactors = FALSE)

我希望颜色为max_choice,并且希望使用per_max为渐变着色。有没有更好的方法可以解决这个问题?

1 个答案:

答案 0 :(得分:0)

因此,我可以在美学内使用alpha = per_max淡化地图。

states%>%
  full_join(soft_drinks_plot, by = c("region" = "State"))%>%
  ggplot()+
  geom_polygon(aes(x = long, y = lat, group = group, 
fill = max_choice, alpha = per_max), color = "black")

有什么办法使它更像放射状的阴影吗?