Alpha value being applied to entire legend

324 views
Skip to first unread message

Joran

unread,
Jun 4, 2010, 5:58:40 PM6/4/10
to ggplot2
I've posted about a related issue before, which I was able to resolve
kind of, but it's popped up again with a slightly different graph.

I just want to know if I'm specifying the graph wrong, or what,
because this seems like really weird behavior:


test_data <-
data.frame(x=rnorm(100),y=rnorm(100),cat1=factor(rep(c(1,2),times=c(50,50))),cat2=rnorm(100))
ggplot(test_data,aes(x=x,y=y)) +
geom_point(aes(colour=cat1,size=cat2),alpha=1/2)

versus

ggplot(test_data,aes(x=x,y=y)) +
geom_point(aes(colour=cat1,size=cat2))

In the first version, the alpha value is clearly being applied to the
_entire_ legend, even the size scale. Am I placing the alpha
parameter in the wrong place?

Is there no way to force the alpha parameter to be applied only to the
data points themselves, not the legend?

Mark Connolly

unread,
Jun 5, 2010, 2:28:00 PM6/5/10
to Joran, ggplot2
Would you be defeating the intent of the legend as a reflection of the
plot? The behavior seems expected rather than weird to me.

I _think_ that the only way to do this is with grids and viewports. You
could capture a legend of a plot without alpha set, create an
appropriately dimensioned grid, print a plot (with alpha set and without
a legend) to a viewport, print the captured legend to another viewport
on the grid. Maybe a better way will show up in the responses.

Mark

Joran

unread,
Jun 10, 2010, 4:45:51 PM6/10/10
to ggplot2
Sorry for disappearing...I've been busy...

On Jun 5, 11:28 am, Mark Connolly <mark_conno...@acm.org> wrote:
> Would you be defeating the intent of the legend as a reflection of the
> plot?  The behavior seems expected rather than weird to me.

Generally, I'd agree. I think I could make a strong case, though,
that alpha blending is an exception and should _never_ be applied to
the legend. The legend should display the color I've chosen, not 1/20
(or whatever) of the color I've chosen. I can't imagine a situation
where I'd actually want very dim, faintly colored items in my legend.
Additionally, you'll notice in the example code I gave, the alpha
blending is applied even to the size scale, which has nothing to do
with the colors scale being used. Why dim every single symbol in the
legend? That just makes no sense to me, but I'm certainly open to
correction...

I suppose one option would be to actually add an entirely new legend
scale every time you use alpha blending that shows the depth of color
changes as items are overplotted, and this would be separate from any
color scale, which would be displayed with the "true" colors.

But in any case, at least I think your answer confirms that there is
no (easy) way to do what I want, at least at the moment...

Thanks,

Joran

Hadley Wickham

unread,
Jun 11, 2010, 10:38:41 AM6/11/10
to Joran, ggplot2
> Generally, I'd agree.  I think I could make a strong case, though,
> that alpha blending is an exception and should _never_ be applied to
> the legend.  The legend should display the color I've chosen, not 1/20
> (or whatever) of the color I've chosen.  I can't imagine a situation
> where I'd actually want very dim, faintly colored items in my legend.
> Additionally, you'll notice in the example code I gave, the alpha
> blending is applied even to the size scale, which has nothing to do
> with the colors scale being used.  Why dim every single symbol in the
> legend?  That just makes no sense to me, but I'm certainly open to
> correction...

The underlying principle of the legends is to make them appear as
similar as possible as the glyphs that actually occur on the plot. I
agree with you that this isn't always what you want for alpha
blending, but I'm not willing to make a special case for a single
aesthetic. What legends really need is some way to override the
defaults so that you can supply the other aesthetics yourself. I've
added a ticket to that affect:
http://github.com/hadley/ggplot2/issues/issue/109

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages