ThesisStudent
ThesisStudent

Reputation: 39

Smooth kernell density graph

I am trying to create a graph of a posterior probability. The code that I'm using is:

qplot(N2016, geom="density", xlim=c((n_star_2016 -5), (CIs2016[[3]])),
      xlab="Número estimado del total de asesinatos 2016",
      ylab="Probabilidad de la estimación", adjust=5)

where N2016 is the posterior probability by 100.000 samples.

The issue that I have is that when I use the "density" kind of plot I get some probabilities at values that are not credible, because my observations are 160. Therefore, I should get a probability of zero before 160.

Graph with adjust=5

img1

I have been playing with the adjust parameter but this does not seem to be the answer, since the curve is not smooth at all.

Graph with adjust=0.25

img2

I'm also attaching the graph using histograms to show that the problem is not with data:

Histogram

img3

Does anyone know how to obtain a smooth-looking curve that does not start before the observations? In this case 160?

Thanks!

Upvotes: 1

Views: 107

Answers (1)

Axeman
Axeman

Reputation: 35177

Use the trim parameter:

r <- rnorm(100)
ggplot() + geom_density(aes(r), trim = FALSE) + xlim(-4, 4),
ggplot() + geom_density(aes(r), trim = TRUE) + xlim(-4, 4)

enter image description here

Upvotes: 3

Related Questions