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
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"),
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/
Do you know how to create a proper legend in this case? Is it even
possible, as there is no colour scale?
Thanks,
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
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"
)