Reputation: 589
I have been plotting density plots for various variables in a dataset of harvested animals. After finally having succeeded in plotting density plots of one numeric variable I moved to the next variable, and then got the warning message: "Removed 684 rows containing non-finite values (stat_density)". I first tried to remove all NA's but that didn't help. Any ideas?
My script:
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()
And a sample of the data frame:
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")
Upvotes: 1
Views: 3963
Reputation: 579
That warning often occurs when you have set limits on an axis, which is also the case here. Running without that line:
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()
The code still returns that warning, but only for the three NA
's that are in the data sample that you provided.
Hope this helps.
Upvotes: 1