【问题标题】:How can I change the color of my statebins map?如何更改 statebins 地图的颜色?
【发布时间】:2020-05-12 18:45:28
【问题描述】:

我正在尝试更改我的 statebins 地图的颜色。我对 R 非常陌生,仍然学到很多东西,谷歌搜索并没有真正帮助。

示例如下:

   fipst stab                state color
1     37   NC       North Carolina     2
2      1   AL              Alabama     2
3     28   MS          Mississippi     2
4      5   AR             Arkansas     2
5     47   TN            Tennessee     2
6     45   SC       South Carolina     1
7     23   ME                Maine     2
49    32   NV               Nevada     1
50    15   HI               Hawaii     2
51    11   DC District of Columbia     2

 digitaltax <- structure(list(fipst = c(37L, 1L, 28L, 5L, 47L, 45L, 23L, 32L,15L, 11L), stab = c("NC", "AL", "MS", "AR", "TN", "SC", "ME","NV", "HI", "DC"), state = c("North Carolina", "Alabama", "Mississippi","Arkansas", "Tennessee", "South Carolina", "Maine", "Nevada","Hawaii", "District of Columbia"), color = c(2L, 2L, 2L, 2L,2L, 1L, 2L, 1L, 2L, 2L)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L,7L, 49L, 50L, 51L), class = "data.frame") 

    ==X==============================================================X==

        mutate(
      digitaltax, 
      share = cut(color, breaks = 3, labels = c("No sales tax", "Exempts digital goods", "Taxes digital goods"))
    ) %>% 
      statebins(
        value_col = "share", font_size = 2.5,
        ggplot2_scale_function = scale_fill_brewer,
        name = ""
      ) +
    labs(title = "Which states tax digital products?") + theme_statebins()

这会生成一张带有一系列蓝色的地图。我怎样才能改变颜色?无论我在谷歌上尝试过什么,它总是抛出这个错误:

Error in ggplot2_scale_function(...) : could not find function "ggplot2_scale_function"

任何帮助都将非常感激。谢谢!

【问题讨论】:

  • 嗨,凯西,欢迎来到 Stack Overflow。 ggplot2_scale_function 不是函数。也许您正在阅读建议您使用 ggplot2 中的比例函数的地方?如果您至少可以提供digitaltax 的样本,我们可能会提供帮助。你可以试试dput(digitaltax)。您可以编辑您的问题并粘贴输出。您可以用三个反引号 (```) 将其括起来以获得更好的格式。请参阅How to make a reproducible example 了解更多信息。
  • 对于颜色,请尝试使用one of the color palettes here。我不熟悉statebins(),但在文档中似乎有一个选项可以指定颜色,如brewer_pal = "PuBu"
  • @Ian Campbell,我添加了一个示例 - 感谢您提供所有这些重要信息。我希望这会有所帮助!
  • 我花了很长时间才发现您使用的是statebins 的开发版本。将来,如果您使用的版本不在 CRAN 中,请在问题中提及。
  • 对不起@Ian-Campbell 我看到github版本有她正在使用的参数,所以先去了。

标签: r colors data-visualization


【解决方案1】:

一种方法是使用命名RColorBrewerbrewer_pal =

library(statebins)
statebins(digitaltax,
          value_col = "color",
          breaks = length(unique(digitaltax$share)),
          labels = unique(digitaltax$share),
          brewer_pal = "Dark2") +
  labs(title = "Which states tax digital products?") 

执行此命令查看所有味觉:

library(RColorBrewer)
display.brewer.all()

【讨论】:

    【解决方案2】:

    使用您的数据和大部分代码,我确实将 DC 颜色更改为 3,以便显示所有类别。

    library(dplyr)
    library(ggplot2)
    library(statebins)
    
    # changes DC to be color 3
    digitaltax <- structure(list(fipst = c(37L, 1L, 28L, 5L, 47L, 45L, 23L, 32L,15L, 11L), stab = c("NC", "AL", "MS", "AR", "TN", "SC", "ME","NV", "HI", "DC"), state = c("North Carolina", "Alabama", "Mississippi","Arkansas", "Tennessee", "South Carolina", "Maine", "Nevada","Hawaii", "District of Columbia"), color = c(2L, 2L, 2L, 2L,2L, 1L, 2L, 1L, 2L, 3L)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L,7L, 49L, 50L, 51L), class = "data.frame")
    
    mutate(
      digitaltax, 
      share = cut(color, breaks = 3, labels = c("No sales tax", "Exempts digital goods", "Taxes digital goods"))
    ) %>% 
      statebins(
        value_col = "share", font_size = 2.5,
        ggplot2_scale_function = scale_fill_brewer,
        name = ""
      ) +
      labs(title = "Which states tax digital products?") + 
      theme_statebins()
    

    reprex package (v0.3.0) 于 2020 年 5 月 12 日创建

    【讨论】:

    • 感谢您抽出宝贵时间提供帮助。是的,这基本上是我得到的蓝调范围的地图。你能以某种方式改变调色板吗?我也添加了我的数据。
    • 问题一秒钟是ggplot2_scale_function = ggplot2::scale_fill_distiller,我会使用你的数据
    猜你喜欢
    • 2014-10-25
    • 1970-01-01
    • 2012-04-21
    • 2020-06-10
    • 2011-01-29
    • 1970-01-01
    • 2014-12-24
    • 2018-11-11
    • 1970-01-01
    相关资源
    最近更新 更多