Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot density with ggplot2 without line on x-axis

I use ggplot2::ggplot for all 2D plotting needs, including density plots, but I find that when plotting a number of overlapping densities with extreme outliers on a single space (in different colors) the line on the x-axis becomes a little distracting.

My question is then, can you remove the bottom section of the density plot from being plotted? If so, how?

You can use this example:

library(ggplot2)
ggplot(movies, aes(x = rating)) + geom_density()

enter image description here

Should turn out like this:

enter image description here

like image 470
mlegge Avatar asked Jul 23 '15 20:07

mlegge


2 Answers

How about using stat_density directly

 ggplot(movies, aes(x = rating)) + stat_density(geom="line")

enter image description here

like image 94
MrFlick Avatar answered Nov 16 '22 22:11

MrFlick


You can just draw a white line over it:

ggplot(movies, aes(x = rating)) +
    geom_density() +
    geom_hline(color = "white", yintercept = 0)

enter image description here

like image 40
Gregor Thomas Avatar answered Nov 17 '22 00:11

Gregor Thomas