geom_bar and legend challenges

278 views
Skip to first unread message

Stanley Chu

unread,
Mar 14, 2010, 1:05:40 PM3/14/10
to ggplot2
Hi everyone,

My goal is to show the difference in magnitude of two time series at
each point in time. What I did is to overlay one geom_bar plot on top
of another, using color and level of transparency to separate two
series.

My biggest question is how do I create a proper legend to indicate
"who's who"? As you see the result of my code, the legend seems to be
messed up by my "alpha" setting...

Also, is there a simpler way (one-step) to get to the same plot?

The reproducible code follows.
#=========================================
dates <- seq(from = as.Date(head(lead.lag.ret$Date, 1)),
to = as.Date(tail(lead.lag.ret$Date, 1)),
by = "year"
)
df <- data.frame(
Date = as.character(dates),
A = rnorm(length(dates)),
B = rnorm(length(dates))
)

p <- ggplot(data = df) +
geom_bar(aes(x = Date, y = A, alpha = 0.5))

p <- p + geom_bar(aes(x = Date, y = B, fill = "red", alpha = 0.5))
p
#=========================================

Any feedback is appreciated. Thanks.
Stanley

Stanley Chu

unread,
Mar 14, 2010, 8:58:51 PM3/14/10
to ggplot2
The code wasn't reproducible... my bad.

Here's the reproducible:
#====================================================================
library(ggplot2)
dates <- seq(from = as.Date("2003-12-31"),
to = as.Date("2009-12-31"),


by = "year"
)
df <- data.frame(
Date = as.character(dates),
A = rnorm(length(dates)),
B = rnorm(length(dates))
)

p <- ggplot(data = df) +
geom_bar(aes(x = Date, y = A, alpha = 0.5))

p <- p + geom_bar(aes(x = Date, y = B, fill = "red", alpha = 0.5))
p

#====================================================================

On Mar 14, 1:05 pm, Stanley Chu <chd...@gmail.com> wrote:
> Hi everyone,
>
> My goal is to show the difference in magnitude of two time series at
> each point in time. What I did is to overlay one geom_bar plot on top
> of another, using color and level of transparency to separate two
> series.
>
> My biggest question is how do I create a proper legend to indicate
> "who's who"? As you see the result of my code, the legend seems to be
> messed up by my "alpha" setting...
>
> Also, is there a simpler way (one-step) to get to the same plot?
>
> The reproducible code follows.
> #=========================================

> dates <- seq(from = as.Date("2003-12-31"),
>     to = as.Date("2009-12-31"),

hadley wickham

unread,
Mar 14, 2010, 10:03:10 PM3/14/10
to Stanley Chu, ggplot2
I suspect you want:

ggplot(data = df) +
geom_bar(aes(x = Date, y = A), alpha = 0.5) +
geom_bar(aes(x = Date, y = B), fill = "red", alpha = 0.5)

Note that the alpha and fill colour do not need to be scaled, so they
are outside the aesthetic mapping.

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

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

Stanley Chu

unread,
Mar 15, 2010, 10:28:06 AM3/15/10
to ggplot2
Thanks Hadley for the tip.

Do you know how to create a proper legend in this case? Is it even
possible, as there is no colour scale?

Thanks,
Stanley

hadley wickham

unread,
Mar 15, 2010, 10:40:01 AM3/15/10
to Stanley Chu, ggplot2
Hi Stanley,

If you want a legend, then:

ggplot(data = df) +
geom_bar(aes(x = Date, y = A, fill = "positive")) +
geom_bar(aes(x = Date, y = B, fill = "negative")) +
scale_fill_manual("Direction", values =
c("positive" = alpha("black", 0.5), "negative" = alpha("red", 0.5)))

Hadley

Brandon Farr

unread,
Mar 15, 2010, 9:37:15 PM3/15/10
to ggplot2
Or, incorporating "melt", and making the ggplot call simpler:

dfm <- melt(
df,
id.vars = "Date",
variable_name = "Entity"
)

ggplot(data = dfm) +
geom_bar(
aes(x = Date, y = value, fill = Entity),
alpha = 0.5,
position = "identity"
)

Stanley Chu

unread,
Mar 31, 2010, 12:02:58 PM3/31/10
to ggplot2
Elegant!
Reply all
Reply to author
Forward
0 new messages