[英]How to set geom_density_2d_filled as background in gganimate
[英]Issues with density plots in R using geom_density_2d_filled
我有x和y位置,我想使用 ggplot 繪制密度 map 但是,它給了我以下錯誤:
seq_len(n) 中的錯誤:參數必須強制為非負 integer 另外:警告消息:1:
stat_density2d_filled()
中的計算失敗:帶寬必須嚴格為正 2:在 min(x, na.rm = na.rm ): 無非缺失 arguments 至最小值; 返回 Inf 3: In max(x, na.rm = na.rm): no non-missing arguments to max; 返回 -Inf 4: In max(f): no non-missing arguments to max; 返回-Inf
x和y都是數字,沒有缺失值。 但我仍然不斷收到同樣的錯誤。 我正在使用的代碼是:
ggplot(Fish, aes(x=xpos, y=ypos)) +
geom_density_2d_filled(aes(fill = ..level..), alpha=0.85, breaks= c(0,10^-5, 10^-4,10^-3,10^-2,10^-1,1),
contour_var = "ndensity") +
scale_fill_brewer(type = "seq",palette = "Spectral", direction = -1)
此代碼適用於其他數據集,其中我有其他魚類的x和y位置。 但是這個數據集給出了錯誤。
帶有數據的 CSV 文件的鏈接是: 鏈接
任何幫助,將不勝感激。
謝謝
這是您的數據問題,而不是代碼問題。 在ypos = 30
(~82%) 和ypos==0
處存在不成比例的ypos
值。 這使其計算密度的能力變得復雜(可能產生NaN
或Inf
,因為seq_length()
需要一個數字)。
如果您刪除此問題,您粘貼的代碼確實有效,因此我建議您仔細檢查數據或導致數據的代碼,而不是此代碼本身。
Fish <- read.csv(".../fish.csv")
NewFish <- Fish[Fish$ypos>0 & Fish$ypos<30,]
ggplot(Fish, aes(x = xpos, y = ypos)) +
geom_density_2d_filled(aes(fill = ..level..), alpha=0.85, breaks= c(0,10^-5, 10^-4,10^-3,10^-2,10^-1,1),
contour_var = "ndensity") +
scale_fill_brewer(type = "seq",palette = "Spectral", direction = -1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.