alpha of color in geom_density

1,004 views
Skip to first unread message

McCreight

unread,
Sep 26, 2010, 2:05:35 AM9/26/10
to ggplot2
i have exactly the last example on this page

http://had.co.nz/ggplot2/geom_histogram.html

the example has alpha=.2 but this appears only to affect the fill. I
want alpha to apply to the color as well. is this possible?

fernando

unread,
Sep 26, 2010, 3:23:24 AM9/26/10
to McCreight, ggplot2
Hi,

As a workaround I think you can just avoid drawing the line.

Hope this helps,
fernando

ggplot(diamonds, aes(depth, fill = cut)) +
geom_density(linetype = 0, alpha = 0.2) + xlim(55, 70)

-----Mensaje original-----
De: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] En nombre de
McCreight
Enviado el: domingo, 26 de septiembre de 2010 8:06
Para: ggplot2
Asunto: alpha of color in geom_density

http://had.co.nz/ggplot2/geom_histogram.html

--
You received this message because you are subscribed to the ggplot2 mailing
list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
Sep 26, 2010, 5:14:21 AM9/26/10
to fernando, McCreight, ggplot2
Hi:

Using color = 'transparent' gives the same result as Fernando's code:


ggplot(diamonds, aes(depth, fill = cut)) +
  geom_density(color = 'transparent', alpha = 0.2) + xlim(55, 70)

Both ideas also work on a related example from a few days ago apropos bar charts:

test <- data.frame(x = letters[1:10], y = rnorm(10),
                   z = sample(letters[11:14], 10, replace=TRUE))
# The 'usual' legend
ggplot(test, aes(x=x, y=y,fill=z)) +
     facet_grid(.~z, scales="free") + geom_bar(color="black")

# linetype = 0:
ggplot(test, aes(x=x, y=y,fill=z)) +
      facet_grid(.~z, scales="free") +
      geom_bar(linetype = 0)
# color = 'transparent'
ggplot(test, aes(x=x, y=y,fill=z)) +
      facet_grid(.~z, scales="free") +
      geom_bar(color="transparent")

I expect these would work with histograms, too.

HTH,
Dennis

James McCreight

unread,
Sep 26, 2010, 12:11:02 PM9/26/10
to Dennis Murphy, fernando, ggplot2
Thanks guys. Unfortunately, I already tried that basic approach and it's unacceptable because it really misrepresents the data when the density is very pointy. I'm using the 'scaled' statistic and the greatest density should touch 1 which it does when color is used. when color is not used, as in your suggestions, the remaining fill only goes to .9. So I actually need the ability to set the alpha of the color if at all possible. Another reason I have is because there is a line in the background which i want to be somewhat visible.

I guess i'm just curious why alpha applies to fill and not color. I makes sense but it seems like alpha could be applied to any or aesthetics. 
--
-
******************************************************************************
James McCreight                                  mccreigh @at@ colorado.edu
NASA Earth Science Fellow
PhD Candidate, Water Resources and Environmental Fluid Mechanics
University of Colorado, Boulder
cell: (831) 261-5149
VoIP (to cell): (720) 897-7546

Brandon Hurr

unread,
Sep 26, 2010, 12:24:55 PM9/26/10
to James McCreight, Dennis Murphy, fernando, ggplot2
I don't have your data so I can't say for certain if this would have an effect, but geom_density() has a size setting as well. Does setting size to 0 make a difference? I would expect that if the border line was really thin that the fill would be slightly larger to compensate. 

It seemed to improve things when I set it to 0.0000001, but it's really hard to tell with the diamond data. 

Brandon

James McCreight

unread,
Sep 26, 2010, 1:03:13 PM9/26/10
to Brandon Hurr, Dennis Murphy, fernando, ggplot2
my apologies, i retract my earlier statement about those approaches not working. i had to display the output differently (zoom way in) to see the top most region of fill going to the upper limit=1.

It appears that all these suggestions work and that i actually had it right before i wasted all your time. 

thanks again. 
Reply all
Reply to author
Forward
0 new messages