smooth colour gradient for density

637 views
Skip to first unread message

Anne Schild

unread,
Apr 11, 2012, 5:47:53 AM4/11/12
to ggplot2
Hi all,

I'm trying to produce density strips in ggplot2. However, the gradient
isn't smooth, but the result is somewhat striped. How can I change
that?

df2 <- data.frame(study = rep(c(1,2,3),each=100),
dens = c(rnorm(100, -0.8, sd=sqrt(0.05)),
rnorm(100, 0.07, sd=sqrt(0.06)),
rnorm(100, 0.45, sd=sqrt(0.049))))


dens2 <- ggplot(df2, aes(x=dens, y=factor(study))) +
stat_density(aes(fill=..density..), geom="tile",
position="identity") +
scale_fill_gradient(low = "white", high = "black") +
facet_grid(study~., scales = 'free_y', space = 'free')
dens2

Cheers,
die Anne.

Jean-Olivier Irisson

unread,
Apr 11, 2012, 10:26:30 AM4/11/12
to Anne Schild, ggplot2
On 2012-Apr-11, at 11:47 , Anne Schild wrote:
>
> I'm trying to produce density strips in ggplot2. However, the gradient
> isn't smooth, but the result is somewhat striped. How can I change
> that?

I would guess that the (very faint) stripes come from the estimation of density which is done at a fixed number of points (512 by default). Unfortunately you cannot adjust it through stat_density so you'll probably have to recompute the density yourself if 512 is not enough for you.

Here is a simpler example showing the effect of the number of points at which density is estimated:

data <- rnorm(100, -0.8, sd=sqrt(0.05))

dens <- density(data)
dens <- data.frame(x=dens$x, density=dens$y)

dens30 <- density(data, n=30)
dens30 <- data.frame(x=dens30$x, density=dens30$y)

dens1000 <- density(data, n=1000)
dens1000 <- data.frame(x=dens1000$x, density=dens1000$y)

ggplot(dens) + geom_tile(aes(x=x, y=1, fill=density)) + scale_fill_gradient(low = "white", high = "black")
ggplot(dens30) + geom_tile(aes(x=x, y=1, fill=density)) + scale_fill_gradient(low = "white", high = "black")
ggplot(dens1000) + geom_tile(aes(x=x, y=1, fill=density)) + scale_fill_gradient(low = "white", high = "black")

I personally see very little difference between the default (512) and 1000, even on a very good and colour-calibrated screen, so I doubt this would show in real life, especially on print.

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/


Anne Schild

unread,
Apr 18, 2012, 3:54:47 AM4/18/12
to ggplot2
Hi Jean-Olivier,

thanks for the example. I assumed it was something along those lines.
I agree with you that the stripes are barely visible, however, in a
blown-up version they become very visible and the actual meaning of
the plot is lost on the viewer. But I guess that it's fine for
standard size graphs.

Cheers
Anne Schild

On Apr 11, 4:26 pm, Jean-Olivier Irisson <iris...@normalesup.org>
wrote:

Anne Schild

unread,
Apr 18, 2012, 11:35:30 AM4/18/12
to ggp...@googlegroups.com, iri...@normalesup.org

Dear all,


I have found the solution and just wanted to share it. In fact, the problem was not directly R-related, but it's got something to do with my pdf viewer or rather graphical settings in general (it also affects the R plotting window). That is also why Jean-Olivier did not see a big difference between the plots, but I still did. I've attached a screen shot to show what I meant when I talked about stripes.

I found the solution here: http://r.789695.n4.nabble.com/ggplot-with-geom-tile-td4176038.html

When using a different pdf viewer the result was fine.


Cheers

Anne.

> > Observatoire Océanologique

density_striped.jpeg
Reply all
Reply to author
Forward
0 new messages