我正在尝试在一个图中绘制两个´geom_vline()‘。
下面的代码可以很好地处理一条垂直线:
x=1:7
y=1:7
df1 = data.frame(x=x,y=y)
vertical.lines <- c(2.5)
ggplot(df1,aes(x=x, y=y)) +
geom_line()+
geom_vline(aes(xintercept = vertical.lines))但是,当我通过更改添加第二条所需的垂直线时
vertical.lines <- c(2.5,4),我得到了错误:
´Error: Aesthetics must be either length 1 or the same as the data (7): xintercept´我该怎么解决这个问题呢?
发布于 2019-02-07 00:20:33
ggplot(df1, aes(x = x, y = y)) +
geom_line() +
sapply(vertical.lines, function(xint) geom_vline(aes(xintercept = xint)))发布于 2019-02-07 15:30:01
使用+ geom_vline时只需删除aes()即可
ggplot(df1,aes(x=x, y=y)) +
geom_line()+
geom_vline(xintercept = vertical.lines)它不工作,因为第二个aes()与第一个冲突,这与ggplot的语法有关。正如错误告诉您的那样,所有的aesthetics都需要具有相同的长度。
您应该将+geom_vline看作是图形的注释层,而不是像+geom_points或+geom_line那样将数据映射到绘图。(请参阅here如何在两个不同的部分中介绍它们)。
数据:
x=1:7
y=1:7
df1 = data.frame(x=x,y=y)
vertical.lines <- c(2.5,4)https://stackoverflow.com/questions/54558000
复制相似问题