[英]how to extract break points created by cut_interval function in ggplot2?
[英]Wrong Number of Groups in Choropleth Using sf, ggplot, and cut_interval()
我試圖控制使用在地區分布類別數sf
, 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" )
在一些數據集中,此代碼工作正常。 有時我可以通過在cut_interval()
選擇說n = 6來解決這個問題,以獲得5組。 但是,我發現經常無法控制等值區域中的群體數量,這對我來說至關重要。 到目前為止,我無法判斷我的數據是否有問題,我的代碼或是否存在軟件錯誤。
在這種情況下, cut_interval()
正確執行,但其中一個切割中沒有觀察到,因此ggplot()
忽略它。
(恰好是中間間隔 - 您實際上可以看到第二個和第三個圖例項之間的覆蓋差距。)
您可以通過使用table()
查看cut_interval()
來驗證這一點:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.