首页 > 解决方案 > 修改对象时分配的变量正在改变 - ggplot

问题描述

我遇到了 ggplot 的问题。

我有一个x1分配给变量的 ggplot 对象y1

然后我修改x1,并将其分配给y2.

由于我更改为x1,y1也更改了。这让我有点失望,不确定我是否理解变量在 R 中的链接方式。示例如下:

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? 

任何输入表示赞赏。

标签: rvariablesggplot2

解决方案


推荐阅读