使用函数添加geom_vline,其中xintercept来自矢量,而非ggplot数据

使用函数添加geom_vline,其中xintercept来自矢量,而非ggplot数据,r,ggplot2,R,Ggplot2,我想在ggplot上画多条虚拟线,这些位置来自一个单独的向量 library(ggplot2) carats <- c(2.5, 0.1) ggplot(diamonds, aes(carat)) + geom_histogram() + geom_vline(aes(xintercept = carats[1]), col = "black", linetype = "dotted", size = 0.5) + geom_vline(aes(xintercept = c

我想在ggplot上画多条虚拟线,这些位置来自一个单独的向量

library(ggplot2)

carats <- c(2.5, 0.1)

ggplot(diamonds, aes(carat)) +
  geom_histogram() +
  geom_vline(aes(xintercept = carats[1]), col = "black", linetype = "dotted", size = 0.5) +
  geom_vline(aes(xintercept = carats[2]), col = "black", linetype = "dotted", size = 0.5)
这给了我一个错误:

eval(expr、envir、enclose)中出错:未找到对象“line\u value”

如何指定我的函数来处理不在ggplot env中的外部向量?

aes()
用于从数据框中的列映射数据。您没有数据帧,
\u vline
/
\u hline/
/
\u abline
甚至显示
xintercept
yintercept
slope
截距
aes()之外。这在
aes()
中也可以正常工作,前提是您为调用提供了
数据集,但您没有

library(ggplot2)

carats <- c(2.5, 0.1)

ggplot(diamonds, aes(carat)) +
  geom_histogram() +
  geom_vline(xintercept = carats, col = "black", linetype = "dotted", size = 0.5) 
库(ggplot2)
克拉
aes()
用于从数据框中的列映射数据。您没有数据帧,
\u vline
/
\u hline/
/
\u abline
甚至显示
xintercept
yintercept
slope
截距
aes()之外。这在
aes()
中也可以正常工作,前提是您为调用提供了
数据集,但您没有

library(ggplot2)

carats <- c(2.5, 0.1)

ggplot(diamonds, aes(carat)) +
  geom_histogram() +
  geom_vline(xintercept = carats, col = "black", linetype = "dotted", size = 0.5) 
库(ggplot2)

克拉
geom\u vline(xintercept=carats,col=“black”,linetype=“domind”,size=0.5)
这给出了
错误:美学必须是长度1或与数据相同(53940):xintercept
Nope,绝对不是。
geom\u vline(xintercept=carats,col=“black”,linetype=“domind”,size=0.5)
这给出了
错误:美学必须是长度1或与数据相同(53940):xintercept
不,绝对不是。这很好,也很容易!非常感谢!那太好了,太容易了!非常感谢!