0
votes

I am making a presentation and would like to present one line graph (geom_line()) with an appropriate legend. I then want to overlay a new geom_line and add the corresponding legend item. For aesthetic reasons, I want the overlay to not modify the legend location given in the first plot. The effect should be that one is drawing on an existing graph, and adding to its legend.

If I simply using ggplot to first make the first plot and then make a new plot with both lines, the location of the legend changes noticeably.

If I try to make the first plot be the full plot, but setting one of the line sizes to zero, I run into the problem that I can't suppress the legend-item for the size-zero line.

How can I achieve my desired effect with ggplot2?

EDIT:

Here is the code to make the two graphs that I first naively tried.

require(ggplot2)
require(reshape2)

x<-seq(-10,10,length=200)
G <- (1/(sqrt(2*pi))) * exp(-((x)^2)/(2))
G2 <- 2*(1/(pi))*(1/(x^2+1))

df = data.frame(x,G,G2)

ggplot(data = melt(data.frame(x,G),id.vars = 'x'))+
  geom_line(aes(x=x, y=value, color=variable),size=.5)+
  scale_color_manual("Distribution",values=c("orange"),labels=c("Gaussian"))+
  coord_cartesian(ylim = c(0, 1)) 

ggplot(data = melt(data.frame(x,G,G2),id.vars = 'x'))+
  geom_line(aes(x=x, y=value, color=variable),size=.5)+
  scale_color_manual("Distribution",values=c("orange","blue"),labels=c("Gaussian","2Gaussian"))+
  coord_cartesian(ylim = c(0, 1)) 

enter image description here

enter image description here

If it's not clear from these pictures that there is a problem, open up the images from these two links and flip from one to another.

http://rpubs.com/jwg/269311

http://rpubs.com/jwg/269312

NOTICE: The problem is even worse than I first described, since not only is the legend moving but the coordinate axis is moving as well.

Presumably this can be fixed by plotting both and then making its legend-item and the line invisible. Is this a possibility?

2
Please provide some example data, code and output: stackoverflow.com/questions/5963269/…neilfws
Your wish is my command. Let me know if there are any other ways I can improve the question.Lepidopterist
If your presentation format allow for embedded animated GIFs, you could create a nice effect using gganimate. Can post an answer showing that if you're interested.neilfws
@neilfws absolutely!Lepidopterist

2 Answers

2
votes

Here's a solution which will keep everything aligned with the bonus of animation.

library(ggplot2)
library(tidyr)
library(gganimate)

p <- df %>% 
  gather(var, val, -x) %>% 
  ggplot(aes(x, val, frame = var)) + 
    geom_line(aes(color = var, group = var, cumulative = TRUE)) +
    coord_cartesian(ylim = c(0, 1))

gganimate(p, "myplot.gif", "gif")

This should generate a file myplot.gif with this result: enter image description here

1
votes

Not sure if this is what you want, but here goes:

x<-seq(-10,10,length=200)
G <- (1/(sqrt(2*pi))) * exp(-((x)^2)/(2))
G2 <- 2*(1/(pi))*(1/(x^2+1))

df <- data.frame(x,G,G2)
df.plot <- tidyr::gather(df, key = 'variable', value = 'value', -x)

    ggplot(df.plot, aes(x, value, color = variable)) + geom_line() + scale_color_manual(breaks = c("G"), values = c("orange", NA)) + 
coord_cartesian(xlim = c(-10, 10), ylim = c(0,1)) + theme(legend.position = c(0,0)) + 
theme(legend.position = "right",
legend.justification = "top")

ggplot(df.plot, aes(x, value, color = variable)) + geom_line() + scale_color_manual(breaks = c("G", "G2"), values = c("orange", "blue")) + 
coord_cartesian(xlim = c(-10, 10), ylim = c(0,1)) + theme(legend.position = "right",
legend.justification = "top")