2

I want to create a ggplot2 incrementally, where I add a number of lines to an existing plot like this:

pl = ggplot()
pl = pl + somesetup
while(stillhavelines) {
  df = getTheLine(fromsomewhere)
  pl = pl + geom_line(aes(df$x,df$y),linetype=lt,colour=co) ## !!!!
}
ggsave(...)

The odd thing is that if I do this then all the line plots will be shown from one single data frame. I instead I try doing this by assigning each dataframe to its own variable it works.

How can I tell ggplot to actually show the lines from the different data frames? Why does it even do this, it is extremely suprising and unintuitive.

6
  • 1
    This isn’t really how ggplot2 is used (though it’s possible with a lot of effort I guess). Why not build the data incrementally before plotting? For that matter, why build anything incrementally at all? Commented Apr 20, 2017 at 15:05
  • @Konrad Rudolph Because it is good coding practice not to repeat almost identical code over and over. Imagine you want a plot demonstrating how parameters change the pdf of a distribution (like this) for 4 parameters, 5 values each. You do not care about the coordinates being plotted, so there is no inherent need to build a data frame. Rather than writing 4x5=20 times almost the same geom_lines command only varying labels etc (or folding labels, colors, etc into your data frame in a weird way) you might want to use a loop. Commented Jun 14, 2019 at 15:03
  • @0range At no point was I suggesting duplicating code. The plot you linked to is trivially doable without repeated code or loops. Commented Jun 14, 2019 at 15:49
  • @Konrad Rudolph Well, if you find this trivial, I am evidently not good enough at working with R and ggplot2. I had this problem the other day (plus having to arrange four of these plots in a cowplot grid, which requires ggplot2). The only way I could make it work was with a combination of a loop and a function. Commented Jun 14, 2019 at 16:38
  • @0range Here’s the code for the plot: rpubs.com/klmr/levy. For grids you can do the same, adding facet_grid; no cowplot necessary. Commented Jun 14, 2019 at 16:45

1 Answer 1

2

You can define the data that's used in a ggplot layer with the data argument:

pl = pl + geom_line(data = df, aes(x = x, y = y), linetype = lt, colour = co)

If data is not specified it will assume that the layer is actually the same each time so it will only show the most recent one.

I tested it with the following code:

library(ggplot2)

dat <- list(
  data.frame(a = 1:5, b = 8:12), 
  data.frame(a = 11:15, b = 18:22), 
  data.frame(a = 21:25, b = 28:32)
)

p <- ggplot()

i <- 1

while(i <= length(dat)) {
  df <- dat[[i]]
  p <- p + geom_line(data = df, aes(a, b))  
  i <- i + 1
}

p

plot image

Though as Konrad Rudolph stated I'm not sure in what scenario you would want to do this. It is better to organize your data first before plotting it for simpler ggplot code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.