首页 > 解决方案 > R usmap - 如何显示两种不同的颜色:一种用于正值渐变,另一种显示负值

问题描述

以下是我当前的图表代码。如何通过将 0 作为中点来制作两种不同的颜色,例如。正数为红色,负数为蓝色。

 plot_usmap(data = df2, values = "diffProviderPerFFS", color = "white") +
      scale_fill_continuous(name = "Provider Per 1000 FFS(2017-2016)", low="light green",high="dark green", label = scales::comma) +
      theme(legend.position = "right")

你

标签: r

解决方案


显然我没有你的数据,所以我编造了一些。你应该能够得到你想要的scale_fill_gradientn

library(usmap)

df2 <- usmap::statepop
df2$diffProviderPerFFS <- runif(nrow(df2), -0.2, 0.5)

plot_usmap(data = df2, values  = "diffProviderPerFFS", color = "white") +
  scale_fill_gradientn(name    = "Provider Per 1000 FFS(2017-2016)", 
                       colours = c("red", "white", "forestgreen"),
                       breaks  = c(-0.2, 0, 0.2),
                       label   = scales::comma) +
  theme(legend.position = "right")

在此处输入图像描述


推荐阅读