如何为 plot_usmap 指定 bin 颜色?

How to specify bin colors for plot_usmap?

我希望创建一个对色标有更多控制的热图,特别是我想为与特定颜色相对应的值范围设置分箱。

下面我提供了一些示例代码来生成一些数据并绘制图表。问题似乎是它如何将颜色映射到中断,它不是 1:1 对应关系,当我向中断添加更多百分位数时,它似乎拉伸了颜色。

这似乎不是一个大问题,但是当我将其应用于整个美国数据集时,我正在使用的配色方案真的崩溃了。

library(usmap)
library(ggplot2)

fips <- seq(45001,45091,2)
value <- rnorm(length(fips),3000,10000)
data <- data.frame(fips,value)
data$value[data$value<0]=0

plot_usmap(regions='counties',data=data,values="value",include="SC") +
  scale_fill_stepsn(breaks=c(as.numeric(quantile(data$value,seq(.25,1,.25)))),
                  colors=c("blue","green","yellow","red"))

plot_usmap(regions='counties',data=data,values="value",include="SC") +
  scale_fill_stepsn(breaks=c(as.numeric(quantile(data$value,seq(0,1,.1)))),
                    colors=c("blue","green","yellow","red"))

#data not provided for this bit
plot_usmap(regions='counties',data=datar,values="1969",exclude=c("AK","HI")) +
  scale_fill_stepsn(breaks=c(as.numeric(quantile(datar$`1969`,seq(0,1,.1)))),
                    colours=c("blue","green","yellow","red"))

一种方法是手动对百分位数进行分类,然后使用因子水平进行手动分隔和标记。

我从来没有使用过 usmap 的这个高级函数,所以我不知道如何处理出现的这个警告。个人更喜欢并推荐使用 ggplot + geom_polygon 或朋友以获得更多控制权。

library(usmap)
library(ggplot2)

fips <- seq(45001,45091,2)
value <- rnorm(length(fips),3000,10000)
mydat <- base::data.frame(fips,value)
mydat$value[mydat$value<0]=0

mydat$perc_cuts <- as.integer(cut(ecdf(mydat$value)(mydat$value), seq(0,1,.25)))


plot_usmap(regions='counties',
           data=mydat,
           values="perc_cuts",include="SC") +
  scale_fill_stepsn(breaks= 1:4, limits = c(0,4), labels = seq(.25, 1, .25),
                    colors=c("blue","green","yellow","red"),
                    guide = guide_colorsteps(even.steps = FALSE))
#> Warning: Use of `map_df$x` is discouraged. Use `x` instead.
#> Warning: Use of `map_df$y` is discouraged. Use `y` instead.
#> Warning: Use of `map_df$group` is discouraged. Use `group` instead.

reprex package (v0.3.0)

于 2020-06-27 创建