2
votes

I am trying to plot and compare two sets of decimal numbers, between 0 and 1 using the R package, ggplot2. When I plotted using geom="density" in qplot, I noticed that the density curve goes past 1.0. I would like to have a density plot for the data that does not exceed the value range of the set, ie, all the area stays between 0 and 1.

Is it possible to plot the density between the values 0 and 1, without going past 1 or 0? If so, how would I accomplish this? I need the area of the two plots to be equal between 0 and 1, the range of the data. Here is the code I used to generate the plots.

Right: qplot(precision,data = compare, fill=factor(dataset),binwidth = .05,geom="density", alpha=I(0.5))+ xlim(-1,2)

Left:qplot(precision,data = compare, fill=factor(dataset),binwidth = .05,geom="density", alpha=I(0.5))

1

1 Answers

3
votes

You might consider using a different tool to estimate the density (the built in density functions do not consider bounds), then use ggplot2 to plot the estimated densities. The logspline package has tools that will estimate densities (useing a different algorythm than density does) and you can tell the functions that your density is bounded between 0 and 1 and it will take that into consideration in estimating the densities. Then use ggplot2 (or other code) to compare the estimated densities.