【问题标题】:usmaps R: Use ggplot2 to set bins and manually colorusmaps R:使用 ggplot2 设置 bin 并手动着色
【发布时间】:2021-10-14 02:55:18
【问题描述】:

我正在使用 R 中的 usmaps() 包来创建美国地图。我有这样的数据设置:

Ces_State_Only:

State 1990
Alabama 0.2
Alaska 0.31
Arizona 0.40

我想创建一个叶绿素图。我想将我的数据分为以下几类:0-0.2、0.2-0.25、0.25-0.3、0.3-0.33、0.33-36、>36。

我的代码是:

plot_usmap(data = Ces_State_only, values = "1990") +   scale_colour_gradientn(colours = "Blues", breaks = c(0,.2, .25, 0.3, 0.33, 0.36, 1),limits = c(0, 100))

此代码有效,但无法对我的数据进行分箱。

我还尝试在映射数据之前对数据进行分箱:

Ces_State_only$Ninety_bin <- cut(Ces_State_only$`1990`,breaks = c(0,0.2, 0.25, 0.3, 0.33, 0.36,1))
    
plot_usmap(data = Ces_State_only, values = "Ninety_bin") +   scale_fill_brewer(palette="Blues",
        aesthetics = "fill",
        na.value = "grey50")+   theme(legend.position = "right")

但是,这只会在美国地图上创建三种不同的颜色(对于以下分档:0-0.2、0.2-0.25 和 0.25-0.3)。

有什么建议或想法吗?

提前谢谢你!

【问题讨论】:

    标签: r ggplot2 gis binning usmap


    【解决方案1】:

    问题是默认情况下使用的因子水平会被丢弃。您可以通过在scale_fill_brewer 中设置drop=FALSE 来防止这种情况发生:

    library(usmap)
    library(ggplot2)
    
    plot_usmap(data = Ces_State_only, values = "Ninety_bin") +
      scale_fill_brewer(
        palette = "Blues",
        aesthetics = "fill",
        na.value = "grey50",
        drop = FALSE
      ) +
      theme(legend.position = "right")
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多