ggplot2: establecer valores (no lineales) para alfa

I'd like to plot a mirrored 95% density curve and map alpha to the density:

foo <- function(mw, sd, lower, upper) {
x <- seq(lower, upper, length=500)
dens <- dnorm(x, mean=mw, sd=sd, log=TRUE)
dens0 <- dens -min(dens)
return(data.frame(dens0, x))
}

df.rain <- foo(0,1,-1,1)

library(ggplot2)


drf <- ggplot(df.rain, aes(x=x, y=dens0))+
geom_line(aes(alpha=..y..))+
geom_line(aes(x=x, y=-dens0, alpha=-..y..))+
stat_identity(geom="segment", aes(xend=x, yend=0, alpha=..y..))+
stat_identity(geom="segment", aes(x=x, y=-dens0, xend=x, yend=0, alpha=-..y..))
drf

This works fine, but I'd like to make the contrast between the edges and the middle more prominent, i.e., I want the edges to be nearly white and only the middle part to be black. I've been tampering with scale_alpha() but without luck. Any ideas?

Edit: Ultimately, I'd like to plot several raindrops, i.e., the individual drops will be small but the shading should still be clearly visible.

preguntado el 31 de julio de 12 a las 11:07

3 Respuestas

Instead of mapping dens0 En el correo electrónico “Su Cuenta de Usuario en su Nuevo Sistema XNUMXCX”. alpha, I'd map it to color:

drf <- ggplot(df.rain, aes(x=x, y=dens0))+
   geom_line(aes(color=..y..))+
   geom_line(aes(x=x, y=-dens0, color=-..y..))+
   stat_identity(geom="segment", aes(xend=x, yend=0, color=..y..))+
   stat_identity(geom="segment", aes(x=x, y=-dens0, xend=x, yend=0, color=-..y..))

enter image description here

Now we still have the contrast in color is mainly present in the tails. Using two colors helps a bit (note that the switch in color is at 0.25):

drf + scale_color_gradient2(midpoint = 0.25)

enter image description here

Finally, to include the distribution of the dens0 values, I base the midpoint of the color scale on the median value in the data:

drf + scale_color_gradient2(midpoint = median(df.rain$dens0))

enter image description here

Nota : But however the way you tweak your data, most contrast in your data is in the more extreme values in your dataset. Trying to mask this by messing with a non-linear scale, or by tweaking a color scale like I did, could present a false picture of the real data.

Respondido 31 Jul 12, 12:07

thanks Paul. I've tried all those before, but am not satisfied with the result and I find it important that it can be displayed in black and white. I feel that in my example alpha only ranges from grey to black. I thought about somehow increasing the range of alpha across the whole range from white to black. - die Anne.

Here is a solution using geom_ribbon() instead of geom_line()

df.rain$group <- seq_along(df.rain$x)
tmp <- tail(df.rain, -1)
tmp$group <- tmp$group - 1
tmp$dens0 <- head(df.rain$dens0, -1)
dataset <- rbind(head(df.rain, -1), tmp)
ggplot(dataset, aes(x = x, ymin = -dens0, ymax = dens0, group = group, 
  alpha = dens0)) + geom_ribbon() + scale_alpha(range = c(0, 1))

enter image description here

ggplot(dataset, aes(x = x, ymin = -dens0, ymax = dens0, group = group, 
  fill = dens0)) + geom_ribbon() + 
  scale_fill_gradient(low = "white", high = "black")

enter image description here

See Paul's answer for changing the colours.

dataset9 <- merge(dataset, data.frame(study = 1:9))
ggplot(dataset9, aes(x = x, ymin = -dens0, ymax = dens0, group = group, 
    alpha = dens0)) + geom_ribbon() + scale_alpha(range = c(0, 0.5)) + 
    facet_wrap(~study)

enter image description here

Respondido 01 ago 12, 09:08

Mhm. As soon as I produce several plots, the contours appear jagged and the overall results appears black: n <- 10 dataset5 <- do.call("rbind", replicate(n, dataset, simplify = FALSE)) dataset5$study <- rep(c(1:10), each=998) ggplot(dataset5, aes(x = x, ymin = -dens0, ymax = dens0, group = group, alpha = dens0)) + geom_ribbon() + scale_alpha(range = c(0, 1))+ facet_grid(study~.) - die Anne.

Can you please provide an example of the jagged contours? I don't get them with your code. If the image is to black, then play a bit with the range argument of scale_alpha. 0 = completely transparant, 1 = completly opaque. Or you can add a 'colour = "gray"' or 'colour = "red"' argument to geom_ribbon - Thierry

The jagged contours were due to the fact that I printed the graph to a pixel format. It seems though that one can work around that by using both geom_ribbon y geom_line. - die Anne.

While pondering both your answers I actually found exactly what I was looking for. The easiest way is to simply use scale_colour_gradientn with a vector of greys.

library(RColorBrewer)
grey <- brewer.pal(9,"Greys")

drf <- ggplot(df.rain, aes(x=x, y=dens0, col=dens0))+
 stat_identity(geom="segment", aes(xend=x, yend=0))+
 stat_identity(geom="segment", aes(x=x, y=-dens0, xend=x, yend=0))+
 scale_colour_gradientn(colours=grey)
drf

Respondido 02 ago 12, 12:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.