0

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?

Lepidopterist
  • 391
  • 1
  • 5
  • 21

2 Answers2

2

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

neilfws
  • 32,751
  • 5
  • 50
  • 63
  • I'm getting a warning ("Ignoring unknown aesthetics: cumulative") and an error ("Don't know how to automatically pick scale for object of type function. Defaulting to continuous. Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 400, 1, 0") – Lepidopterist Apr 20 '17 at 02:14
  • The warning about cumulative you can ignore (it works despite the warning). I don't know what the other issue might be; your data as given and my code as given work for me. I use the "latest everything" (R 3.3.3, ggplot2 2.2.1). – neilfws Apr 20 '17 at 02:44
  • I have both R 3.3.3 and ggplot2 2.2.1. Very strange. – Lepidopterist Apr 20 '17 at 02:51
  • It's hard for me to debug because your use of R is fancy to my eyes. I don't understand the iterated %>%, and reading about it doesn't make it obvious what you are doing or why you switched from my use of melt to tidyr. But still it's odd that we're using the same code and not getting the same output. – Lepidopterist Apr 20 '17 at 02:54
  • What type of object is p supposed to be? When I type class(p) I get "1] <<"gg" "ggplot">> output. And it looks to be a very funny kind of object. – Lepidopterist Apr 20 '17 at 02:55
  • 1
    My apologies: there was a typo in my code. I left in the variable "dist" from a previous attempt, it should be "var". Edited, try it again. – neilfws Apr 20 '17 at 03:00
  • 1
    We have liftoff. Thanks! – Lepidopterist Apr 20 '17 at 03:14
  • Great! The other questions: I just prefer pipes and tidyr in my general workflow, melt works too. And p is just a ggplot object; you can assign it to variables as well as direct output to graphics. – neilfws Apr 20 '17 at 03:17
1

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")
ahly
  • 1,121
  • 7
  • 9