r - 使用 sf、ggplot 和 cut_interval() 的 Choropleth 中的组数错误

标签 r ggplot2 choropleth sf

我正在尝试使用 sf 控制 choropleth 中的类别数量, ggplot2cut_interval()ggplot2 .有时它可以工作,但对于某些数据集,类别数会减少 1。下面是我的代码,输入数据集 (7Kb) 在这里:
ggplot-test-04.geojson

library(sf)
library(ggplot2)

lga.sf <-  st_read("ggplot-test-04.geojson")

ggplot() +
  geom_sf(data = lga.sf,
          aes(fill = cut_interval(value,5))) +
  scale_fill_brewer(palette = "RdYlBu", 
                    name = "Legend" )

我试图获得 5 个组,但结果有 4 个:
4 groups

在某些数据集中,此代码工作正常。有时我可以通过在 cut_interval() 中选择 say n=6 来解决这个问题。得到5组。但是,我发现我经常无法控制 choropleth 中的组数量,这对我来说至关重要。到目前为止,我无法判断我的数据是否有问题、我的代码是否存在问题,或者是否存在软件错误。

最佳答案

在这种情况下,cut_interval()执行正确,但其中一个切割中的观察值为零,因此 ggplot()忽略它。

(它恰好是中间间隔 - 您实际上可以看到第二个和第三个图例项之间的覆盖范围。)

您可以通过查看 cut_interval() 来验证这一点。与 table() :

table(cut_interval(lga.sf$value, 5))

[1.44,1.61] (1.61,1.78] (1.78,1.95] (1.95,2.11] (2.11,2.28] 
          3           7           0           1           1 

关于r - 使用 sf、ggplot 和 cut_interval() 的 Choropleth 中的组数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459023/

相关文章:

macos - R包安装在所有机器上成功,但一台

r - 如果加载了 ggplot2 包,则在对象上调用 `str` 会出错

r - 在嵌套的 dplyr 数据集中运行配对 t 检验的问题

r - 具有 Lubridate 持续时间的 Data.frame 摘要

r - 使用 geom_segment 指示 geom_errorbar 超出 log10 比例限制

R ggplot2 : legend should be discrete and not continuous

r - 是否可以添加一个图例框,其中包含用 geom_text 编写的文本?

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

javascript - Mapbox GL JS 创建自定义图例

javascript - 我想在 getcolor 函数传单 js 中进行条件返回