Legend for plot with multiple geoms with groupings and based on multiple datasets

582 views
Skip to first unread message

Leon

unread,
Mar 6, 2012, 5:26:22 PM3/6/12
to ggplot2
I was able to make a pretty Visual Predictive Check plot fairly easily
with a little bit of code from ggplot2. However, I have been unable to
create a legend that indicates that the red solid lines are predicted
quantiles and the grey (azure4) dashed lines are observed 90%
quantiles. Can anyone point me in the right direction? Below is the
code. Thanks in advance for the help.

### Data Preps
dat = Residuals
datSimQ = PredCheck_SimQ
datObsQ = PredCheck_ObsQ_SimQCI
names(datSimQ)
# [1] "Scenario" "Strat" "IVAR" "DV" "ObsName" "Q"
names(datObsQ)
# [1] "Scenario" "Strat" "IVAR" "DV0" "DV"
"ObsName" "QI" "QE"
datSimQWide=cast(datSimQ,IVAR ~ Q, value="DV")
colnames(datSimQWide)=c("IVAR","MIN","MED","MAX")

#Plotting
PlotTitle = "Visual Predictive Check"
pl = ggplot(datSimQWide, aes(IVAR, MED)) +
xlab(paste("Time ", timeunit, sep = " ")) +
ylab(paste("Concentration ", concunit, sep=" ")) +
geom_ribbon(data=datSimQWide, aes(ymin=MIN, ymax=MAX),
fill="darkorange3", alpha=0.5) +
geom_point(data=dat, aes(IVAR, DV), size=3, shape=1, color="azure4")
+
geom_line(data=datObsQ,aes(IVAR, DV0, group=QI),color="azure4",
linetype=2 ) +
geom_line(data=datSimQ,aes(IVAR, DV, group=Q),color="red",
linetype=1 ) +
opts(legend.position = "right") +
opts(title=PlotTitle)
pl



Dennis Murphy

unread,
Mar 6, 2012, 8:16:52 PM3/6/12
to Leon, ggplot2
Hi:

See the examples on pp. 108-109 of the ggplot2 book if you have it.
Here is the relevant code from the ggplot2 book web site:

huron <- data.frame(year = 1875:1972, level = LakeHuron)
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5), colour = "blue") +
geom_line(aes(y = level + 5), colour = "red")

ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above"))

ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above")) +
scale_colour_manual("Direction",
c("below" = "blue", "above" = "red"))

Notice that in the first plot, there is no legend. This is because the
color aesthetic is set to a single color in each geom_line() layer. In
the other two plots, the colour statement is placed inside the aes()
call, which means that color is now being 'mapped'. The values
ascribed to colour are essentially factor levels that are being
created on the fly which will be used as the labels in the legend key.
The scale_colour_manual() line maps specific color values to the
constructed factor levels rather than the defaults.

You'll need to use a similar device in your plot unless you
reconstruct your input data frame appropriately to avoid it. Had you
provided a reproducible example, it would have been easier to show
you, but if you understand the code above, it may not be necessary.

Dennis

> --
> 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

Leon

unread,
Mar 7, 2012, 4:18:37 PM3/7/12
to ggplot2
Hi Dennis,

Thanks a lot for taking the time to respond. I will try this later
today and report back.

Leon
Reply all
Reply to author
Forward
0 new messages