Two datasets with two legends?

83 views
Skip to first unread message

Guillaume T.R.

unread,
Nov 13, 2012, 10:07:15 AM11/13/12
to ggp...@googlegroups.com
Hi!

I am plotting a dataset and adding geom_vline from a second dataset to mark certain actions in time. However, I was unable to find out how to get the legends related to geom_vline and the second dataset.

I'm using something similar to that, execpt that I'm using time as x axis. I would like to have at least a legend with "Action" in the "y2" dataset. Should I include y2 in a melted y1?

Thank you all!

guillaume

-

y1 = data.frame(A = c(1:100), B = rnorm(100), C = rnorm(100), D = rnorm(100))
y1.m = melt(y1, id="A")

y2 = data.frame(.A. = c(10, 50, 80), Press = c(NA, 2 , 3), Action = c(".a.",".b.",".c."))

p = ggplot(y1.m , aes(x=A, y=value, color=variable)) +
         geom_vline(data=y2, aes(xintercept=.A., linetype=Action, alpha=Press)) +
         geom_point() +         
         facet_grid(variable ~ ., scale="free_y")
p




Dennis Murphy

unread,
Nov 13, 2012, 5:50:28 PM11/13/12
to Guillaume T.R., ggp...@googlegroups.com
Hi:

Something seems wrong, as I would have expected a legend for linetype. I did a little fiddling around (thanks for the reproducible example, BTW) and it's clear that ggplot2 is not rendering the legend for linetypes coming from geom_vline() with the 'usual' mantra:

y1 = data.frame(A = c(1:100), B = rnorm(100), C = rnorm(100), D = rnorm(100))
y1.m = melt(y1, id="A")

y2 = data.frame(.A. = c(10, 50, 80), Press = c(NA, 2 , 3), Action = c(".a.",".b.",".c."))

> str(y2)
'data.frame':   3 obs. of  3 variables:
 $ .A.   : num  10 50 80
 $ Press : num  NA 2 3
 $ Action: Factor w/ 3 levels ".a.",".b.",".c.": 1 2 3

# => Action seems OK as an aesthetic for linetype.
# To simplify matters,
y2$A <- y2[[".A."]]    # copy .A. to A

# Reformulate the call so that A is the only 
# common aesthetic among layers, since A is now in both 
# y1.m and y2.  No legend for linetype appears.
ggplot(y1.m, aes(x = A)) +
  geom_vline(data = y2, aes(xintercept = A, linetype = Action)) +
  geom_point(aes(y = value, color = variable)) +
  facet_grid(variable ~ ., scale = "free_y")

# Try to make an explicit legend for linetype...no change.
# (Ditto for scale_linetype_discrete().)
last_plot() + scale_linetype(breaks = levels(y2$Action))

# Redoing the plot and using scale_linetype_manual()
# doesn't help, either:
ggplot(y1.m, aes(x = A)) +
  geom_vline(data = y2, aes(xintercept = A, linetype = Action)) +
  geom_point(aes(y = value, color = variable)) +
  facet_grid(variable ~ ., scale = "free_y") +
  scale_linetype_manual(breaks = levels(y2$Action), values = 1:3)

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

<snip US English locale info>

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.9.2.1 plyr_1.7.1      reshape2_1.2.1 

loaded via a namespace (and not attached):
 [1] colorspace_1.2-0   dichromat_1.2-4    digest_0.5.2       grid_2.15.2       
 [5] gtable_0.1.1       labeling_0.1       MASS_7.3-22        memoise_0.1       
 [9] munsell_0.4        proto_0.3-9.2      RColorBrewer_1.0-5 scales_0.2.2      
[13] stringr_0.6.1

Dennis





--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Guillaume Théroux Rancourt

unread,
Nov 19, 2012, 10:16:22 AM11/19/12
to Dennis Murphy, ggp...@googlegroups.com
Hi Dennis and all!

I've played around with the plot and the problem seems to come from geom_vline and the second data frame, and not the legend for linetype. A legend is generated for the main dataset, but none is generated for geom_vline and the second dataset.

y1 = data.frame(A = c(1:100), B = rnorm(100), C = rnorm(100), D = rnorm(100))
y1.m = melt(y1, id="A")
y2 = data.frame(.A. = c(10, 50, 80), Press = c(NA, 2 , 3), Action = c(".a.",".b.",".c."))
y2$A <- y2[[".A."]]    # copy .A. to A

# This generates a legend for color in geom_point:

ggplot(y1.m, aes(x = A)) +
  geom_vline(data = y2, aes(xintercept = A, linetype = Action)) +
  geom_point(aes(y = value, color = variable)) +
  facet_grid(variable ~ ., scale = "free_y")

# This generates a legend for linetype in geom_line (here replacing geom_point in the previous example):

ggplot(y1.m, aes(x = A)) +
  geom_vline(data = y2, aes(xintercept = A, color = Action)) +
  geom_line(aes(y = value, linetype = variable)) +

  facet_grid(variable ~ ., scale = "free_y")

Is that a bug? Is there a workaround? Is the problem with geom_vline or with a second dataset?

Thank you!

guillaume


2012/11/13 Dennis Murphy <djm...@gmail.com>

Ista Zahn

unread,
Nov 19, 2012, 12:59:03 PM11/19/12
to Guillaume Théroux Rancourt, Dennis Murphy, ggp...@googlegroups.com
Hi guys,

It really is a good idea to read the docs:

?geom_vline

Arguments:
show_guide: should a legend be drawn? (defaults to ‘FALSE’)

So we need


ggplot(y1.m , aes(x=A, y=value, color=variable)) +
geom_vline(data=y2, aes(xintercept=.A., linetype=Action,
alpha=Press), show_guide=TRUE) +
geom_point() +
facet_grid(variable ~ ., scale="free_y")

Best,
Ista
Reply all
Reply to author
Forward
0 new messages