density line cuts negative values using ggplot2

261 Views Asked by At

I'm drawing a weighted density function using ggplot2. I've data from various years (2008 to 2017), and I draw the density function for each year. But in 2 years negative values are cut:

(image)density function with cut negative values (image)density function with cut negative values

ggplot(subset(ECV_CAT0817,HB010==2017)) + 
  geom_density(aes(rendaOCDE, weight=DB090/sum(DB090))) + 
  coord_cartesian(xlim=c(-5000,50000))

The rest of the years negative values appears correctly, for example:

(image)density function with negative values correctly (image)density function with negative values correctly

How can I avoid the cut in negative values using ggplot2?

If I use plot and density functions, the values appears correctly, but I want to use ggplot2.

(image)using plot function (image)using plot function

plot(
  density(
    subset(ECV_CAT0817, HB010==2017)$rendaOCDE, 
    weights=subset(ECV_CAT0817, HB010==2017)$DB090/sum(subset(ECV_CAT0817, HB010==2017)$DB090)),
  xlim=c(-5000,50000))

Thank you very much.

0

There are 0 best solutions below