我正在尝试使用 sf
控制 choropleth 中的类别数量, ggplot2
和 cut_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 个:
在某些数据集中,此代码工作正常。有时我可以通过在
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/