1

I am trying to add a customize legend to ggplot. I am using water colors plot from (http://www.nicebread.de/visually-weighted-watercolor-plots-new-variants-please-vote/). The code for vwReg is present in the website.

I am plotting 2 different data as

 mtcars$rand2 = rnorm(nrow(mtcars))
 mtcars$rand1 = rnorm(nrow(mtcars))
 pp1 = vwReg(rand1~wt, data=mtcars, spag=T, add=T, shade=F, spag.color="green")
 pp2 = vwReg(rand2~wt, data=mtcars, spag=T, add=T, shade=F, spag.color="red")
 ggplot2() + pp1 + pp2

However, it does not give any legend to rand1 and rand2. I tried to add a color asthestic to geom_path that creates spag (spagetti) plot. However the plot is too thin and not visible. I am wondering if there is a way to add a customize-legend as a separate layer and tell it to be have 2 color red and green.
enter image description here

I code for vwReg can be also downloaded from https://dl.dropboxusercontent.com/u/2706915/vwRegs.R

vinash85
  • 431
  • 4
  • 15
  • The example is not reproducible: `Error: could not find function "vwReg"`. You don't say from which package `vwReg` is coming. –  Jul 30 '14 at 03:01
  • @Pascal Sorry for not being clear. The code is present at https://dl.dropboxusercontent.com/u/2706915/vwRegs.R – vinash85 Jul 30 '14 at 03:18
  • 1
    i believe the vcd package has a (basic) grid-based version of legend, called grid.legend. – baptiste Jul 30 '14 at 07:55

2 Answers2

2

You could also do this by changing the vwReg function a bit. Right now, color is being set to spag.color not mapped. If you change this line of the vwReg function:

gg.spag <-  geom_path(data=b2, aes(x=x, y=value, group=B), size=0.7, alpha=10/B, color=spag.color)

To something like this:

gg.spag <-  geom_path(data=b2, aes_q(x=quote(x), y=quote(value), group=quote(B), color=spag.color), size=0.7, alpha=10/B)

you will be able to get legends on your plot (this answer gives an example of mapping colors in this way).

You will have to do a little more work to make the legend look right. First, you have to change legend.position in theme (right now legend.position is set to "none"). Once you allow legends, you'll see there is a continuous legend for alpha that you'll likely want to remove. You can edit the color legend withscale_color_manual.

After editing the vwReg function as above, here is one way to make a figure with the legend with a color for each group.

ggplot() + 
    pp1 + pp2 + 
    theme(legend.position = "right") +
    scale_color_manual(name = "Number of Cylinders", values = c("green" = "green", "red" = "red"), 
                    labels = c("group1", "group2"), 
                    guide = guide_legend(override.aes = list(alpha = 1))) +
    scale_alpha_continuous(guide = FALSE)
Community
  • 1
  • 1
aosmith
  • 34,856
  • 9
  • 84
  • 118
0

I found an ugly ( and completely non-elegant ) way out of this. Its combining ggplot2 and legend with

 library(gridBase)
 plot.new()
 grid.newpage()
 pushViewport(viewport(layout = grid.layout(1, 1)))
 #Draw ggplot
 pushViewport(viewport(layout.pos.col = 1))
 print(ggplot2() + pp1 + pp2, newpage = FALSE)
 legend("topright", inset=.05, title="Number of Cylinders",
    c("rand1", "rand2"), fill=c("blue", "black") , horiz=TRUE)
 popViewport()
vinash85
  • 431
  • 4
  • 15