0

I am running into an issue with ggplot.

I have a ggplot object x1 that I assign to variable y1

I then modify x1, and assign it to y2.

As a result of my change to x1, y1 is also changed. This is throwing me off a bit, not sure if I am understanding how variables are linked in R. Example below:

x1 <- ggplot() + geom_vline(xintercept = 10)
y1 <- x1

# x1 and y1 print plot with vline at 10

x1[["layers"]][[1]][["data"]][["xintercept"]] <- 20
y2 <- x1

 # x1 y2 and y1 print plot with vline at 20
# why is y1 being modified? 

Any input is appreciated.

jack kelly
  • 320
  • 1
  • 8
  • 2
    Does [this](https://stackoverflow.com/questions/25732393/r-ggplot2-strange-behaviour-it-looks-its-passing-by-reference) answer your question? Essentially, it seems that `ggplot` objects are effectively reference types rather than value types. – cazman Oct 06 '21 at 18:30
  • @cazman Yes this explains the issue... I will attempt a few workarounds. Thanks for your input – jack kelly Oct 06 '21 at 18:35

0 Answers0