由于非有限值 (stat_density) 而导致的密度图问题

Problem with density plot due to non-finite values (stat_density)

我一直在绘制收获动物数据集中各种变量的密度图。最终成功绘制了一个数值变量的密度图后,我移至下一个变量,然后收到警告消息:"Removed 684 rows containing non-finite values (stat_density)"。我首先尝试删除所有 NA,但没有帮助。有什么想法吗?

我的脚本:

ggplot(df, aes(x = MEATW,y=..count..,fill=factor(gr))) +
  geom_density(alpha=0.6,size=0.4) +
  scale_x_continuous(limit=c(0,30),expand=c(0,0)) +
  scale_fill_manual(values = c("#d4faad","#868686FF", "#EFC000FF","#BEafad")) +
  theme_bw()

以及数据框样本:

dput(df)
structure(list(MEATW = c(1243, 1500, 813, 900, 800, 800, 500, 
900, 500, 600, 400, 700, 600, 600, 500, 500, 500, 500, 600, 800, 
700, 600, 600, 600, 500, 700, 500, 600, 700, 587, NA, NA, NA, 
650, 600, 650, 650, 600, 600, 650, 600, 600, 600, 650, 650, 600, 
600, 650, 650, 650, 700, 1200, 1100, 900, 900, 1000, 700, 1695, 
915, 850, 1905, 1500, 700, 650, 350, 1563, 1275, 1295, 1000, 
800, 1900, 1100, 850, 900, 1000, 1000, 1200, 800, 1200, 1400, 
700, 700, 1000, 900, 1200, 1100, 326, 1200, 700, 500, 598, 1484, 
1000, 1100, 740, 725, 775, 1300, 935, 832, 860), gr = c(1, 1, 
2, 1, 1, 2, 1, 1, 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 
1, 2, 1, 2, 2, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 
1, 1, 2, 1, 1, 1, 3, 4, 4, 3, 3, 3, 4, 3, 3, 3, 3, 3, 3, 4, 3, 
3, 4, 4, 4, 3, 4, 4, 3, 3, 4, 4, 4, 3, 4, 4, 4, 4, 3, 3, 3, 4, 
4, 3, 4, 3, 4, 3, 4, 4, 4, 4, 4, 3, 3, 4, 3)), row.names = c(1508L, 
1509L, 1510L, 1511L, 1512L, 1513L, 1514L, 1515L, 1516L, 1517L, 
1518L, 1519L, 1520L, 1521L, 1522L, 1523L, 1524L, 1525L, 1526L, 
1527L, 1528L, 1529L, 1530L, 1531L, 1532L, 1533L, 1534L, 1535L, 
1536L, 1537L, 1538L, 1539L, 1540L, 1541L, 1542L, 1543L, 1544L, 
1545L, 1546L, 1547L, 1548L, 1549L, 1550L, 1551L, 1552L, 1553L, 
1554L, 1555L, 1556L, 1557L, 2177L, 2178L, 2179L, 2180L, 2181L, 
2182L, 2183L, 2184L, 2185L, 2186L, 2187L, 2188L, 2189L, 2190L, 
2191L, 2192L, 2193L, 2194L, 2195L, 2196L, 2197L, 2198L, 2199L, 
2200L, 2201L, 2202L, 2203L, 2204L, 2205L, 2206L, 2207L, 2208L, 
2209L, 2210L, 2211L, 2212L, 2213L, 2214L, 2215L, 2216L, 2217L, 
2218L, 2219L, 2220L, 2221L, 2222L, 2223L, 2224L, 2225L, 2226L, 
2227L), class = "data.frame")

当您在轴上设置限制时,通常会出现该警告,此处也是这种情况。 运行 没有那一行:

ggplot(df, aes(x = MEATW,y=..count..,fill=factor(gr))) +
geom_density(alpha=0.6,size=0.4) +
scale_fill_manual(values = c("#d4faad","#868686FF", "#EFC000FF","#BEafad")) +
theme_bw()

代码仍然 returns 该警告,但仅针对您提供的数据样本中的三个 NA

希望对您有所帮助。