mlegge
mlegge

Reputation: 6913

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

Upvotes: 6

Views: 3070

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 146224

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

Upvotes: 2

MrFlick
MrFlick

Reputation: 206596

How about using stat_density directly

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

enter image description here

Upvotes: 12

Related Questions