r - choroplethr 图例未打印某些图,scale_fill_brewer() 的问题

标签 r choropleth choroplethr

我正在尝试制作两张分区统计图。第一个效果很好。但是,当我使用不同的 value 列复制代码/方法时,图例不会打印。两组代码之间的区别在于在第二个图中插入了 scale_fill_brewer() 。是否使用 scale_fill_brewer() 覆盖 c$legend

例如,这有效:

head(ra, 5)
           region value
    1     alabama   106
    2  california   622
    3    colorado    22
    4 connecticut    86
    5    delaware    43
 str(ra)
'data.frame':   51 obs. of  2 variables:
 $ region: chr  "alabama" "california" "colorado" "connecticut" ...
 $ value : num  106 622 22 86 43 7 232 19 10 121 ...

    c = StateChoropleth$new(ra)
    c$legend = "# of stores"
    c$set_num_colors(4)
    c$set_zoom(NULL)
    c$show_labels = FALSE
    without_abbr = c$render()
    without_abbr 

produces

但是,以下结果会导致图例中的“值”:

head(ra, 5)
    region      value
    1    alabama  5.8703474
    2     alaska  0.4880526
    3    arizona  4.8851831
    4   arkansas  2.7045759
    5 california 35.2607419

    > str(ra)
    'data.frame':   51 obs. of  2 variables:

 $ region: chr  "alabama" "alaska" "arizona" "arkansas" ...
 $ value : num  5.87 0.488 4.885 2.705 35.261 ...

    c = StateChoropleth$new(ra)
    c$title = "Total Sales"
    c$legend = "$ billions"
    c$set_num_colors(4)
    c$set_zoom(NULL)
    c$show_labels = FALSE
    without_abbr = c$render()
    without_abbr + scale_fill_brewer(palette=2) # palette 2 is green

enter image description here

最佳答案

感谢您使用 choroplethr。

试试这个:

without_abbr + scale_fill_brewer(name="legend title", palette=2) 

enter image description here

问题在于 ggplot2 如何处理图例(比例)。您可以选择使用 name 参数手动命名比例/图例。否则,看起来它只是使用列的名称。

关于r - choroplethr 图例未打印某些图,scale_fill_brewer() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38509544/

相关文章:

r - 有效地计算跨多列的字符串出现次数

r - 优化具有许多 'layers' 的详细 map 的 Shiny + Leaflet 性能

python-3.x - plotly-express 图形颜色图例应仅显示整数值

r - 一个州等值线图中的县

r - 是否有一个运算符可以代替函数 coord_map() 的 '+' 符号?

r - Choroplethr 中州名称的格式是什么?

r - dplyr: mutate() 和 mutate_if()

r - 在 R 中输出 FALSE 而不是错误

python - Plotly Express 乡村地区的合唱团

r - pivot_longer 与组和删除列名前缀