1

Recently, I encountered a question in ggplot2 field. It's confused for me that everytime I plot first plot with ggplot names "pic1"(the result of running is okay), and then I plotted second one with ggplot2 called "pic2". Of course, the "pic2" is good. But at this moment, I check "pic1", I found the regression line became a vertical line.For example:

  1. "pic1" enter image description here
p <- ggplot()  
p <- p + geom_line(data = MyData, aes(x = otherCrop, y = eta ))
p <- p+ geom_point(data = dat,aes(x =otherCrop,
                                  y = dat$sumEnemies, colour = YEAR ),position = position_jitter(width = .01),size = 1)

 

p <- p+labs(colour = "年份\nYear")    + theme_classic(base_size=18)  +
        theme(axis.title.x=element_text( vjust=0))
p=p + theme(text=element_text(family="Times",   size=18)) 
pic1=p  
  1. "pic2" enter image description here
p <- ggplot()  
p <- p + geom_line(data = MyData, aes(x = SHDI, y = eta ))
p <- p+ geom_point(data = dat,aes(x = dat$SHDI,
                                  y = eta,colour = YEAR ),position = position_jitter(width = .01),size = 1)

 

p <- p+labs(colour = "年份\nYear")    + theme_classic(base_size=18)  +   
        theme(axis.title.x=element_text( vjust=0))
p=p + theme(text=element_text(family="Times",   size=18)) 
pic2=p  

But at this moment, I started to review "pic1", I found it as below: enter image description here It became a strange short vertical line. This would be difficult because I cannot plot them in a same paper. Does anybody know what's the problem?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
PeterPanPan
  • 151
  • 4
  • It is difficult to say exactly what is happening here. It seems like some data is overwritten in the process, and when the first `ggplot` is rewritten (code called again), it does not get the correct information. If it is for a paper in Rmarkdown, one solution is to simply save the plots, and then insert them as images (png, svg, etc.). A slightly unrelated answer of mine [here](https://stackoverflow.com/a/63421494/10782538) shows one method of doing this, while adding more flexibility using latex within the document. Alternative `knitr::include_graphics` can be used with less options. – Oliver Aug 23 '20 at 08:33
  • Can you provide an example using data we have access to? Please, do also include all the packages you are using in the code you provide. An example that anyone can copy and paste into R and run makes it possible for others to investigate the problem. All I can say is that something weird is going on as `gg` (ggplot) objects are self contained and include a copy of the data. – Pedro J. Aphalo Aug 23 '20 at 08:53

1 Answers1

3

I think this is a great example of why using the dataframe$column syntax inside an aes call is discouraged: it makes your plot vulnerable to subsequent changes in your data. Here's a simple example. Start with a data frame with columns x and y:

library(ggplot2)

df <- data.frame(x = 1:10, y = 1:10)

Now make a ggplot, but instead of using aes(x = x, y = y), we make the mistake of doing aes(x = df$x, y = df$y):

vulnerable_plot <- ggplot()
vulnerable_plot <- vulnerable_plot + geom_line(data = df, aes(x = df$x, y = df$y))
pic1 <- vulnerable_plot

Now we review our plot. Sure, ggplot nags us to say we shouldn't use this syntax, but the plot looks fine, so who cares, right?

pic1
#> Warning: Use of `df$x` is discouraged. Use `x` instead.
#> Warning: Use of `df$y` is discouraged. Use `y` instead.

Now, let's make pic2 identical to pic1 except we use the correct syntax:

invulnerable_plot <- ggplot()
invulnerable_plot <- invulnerable_plot + geom_line(data = df, aes(x = x, y = y))
pic2 <- invulnerable_plot

Now we don't get any warning, but the plot looks the same.

pic2

So there's no difference between pic1 and pic2. Or is there? What happens when we change our data frame?

df$y <- 10:1

vulnerable_plot

Oh dear. Our first plot has changed because the plot object has a reference to an external variable that it relies on to build the plot. That's not what we wanted.

However, with the version where we used the correct syntax, a copy of the data was taken and is kept with the plot data, so it remains unaffected by subsequent changes to df:

invulnerable_plot

Created on 2020-08-23 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Year, I absolutely admit that you gave a real solution for this kind of question. According to you advice, I modified my code, and the results are good. Than you very much . THanks all of you who answer my questions. – PeterPanPan Aug 23 '20 at 11:55
  • @PeterPanPan you're welcome. Please feel free to accept this solution if it has solved your issue. – Allan Cameron Aug 23 '20 at 12:23
  • 1
    Excellent example and explanation, @AllanCameron. Others should point to this example when this comes up again... you and I both know that it will! – chemdork123 Aug 23 '20 at 13:33