绘图中的多个vlines给出错误ggplot2 [英] Multiple vlines in plot gives error, ggplot2

查看:147
本文介绍了绘图中的多个vlines给出错误ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想完全按照

I want to do exactly as explained here, the accepted answer does for some reason not work for me.

library("ggplot2")

tmp <- data.frame(x=rep(seq(as.Date(0, origin="1970-01-01"),
                            length=36, by="1 month"), 2),
                  y=rnorm(72),
                  category=gl(2,36))

p <- ggplot(tmp, aes(x, y, colour=category)) +
     geom_line() +
     geom_vline(aes(xintercept=as.numeric(x[c(13, 24)])),
                linetype=4, colour="black")
print(p)

我将图层(geom ="line")与geom_line()交换,以仅使绘图有效.但是,添加vlines时出现此错误:

I exchanged layers(geom="line") with geom_line() to get only the plot working. However, when adding vlines I get this error:

Error: Aesthetics must be either length 1 or the same as the data (72):  xintercept
10: stop("Aesthetics must be either length 1 or the same as the data (", 
    n, "): ", paste(names(!good), collapse = ", "), call. = FALSE)
9: check_aesthetics(evaled, n)
8: f(..., self = self)
7: l$compute_aesthetics(d, plot)
6: f(l = layers[[i]], d = data[[i]])
5: by_layer(function(l, d) l$compute_aesthetics(d, plot))
4: ggplot_build(x)
3: print.ggplot(p)
2: print(p)
1: print(p)

推荐答案

以下内容对我有用:

ggplot(tmp,aes(x, y, colour=category)) +
geom_line() +
geom_vline(data=tmp[c(13,26),],aes(xintercept=as.numeric(x)))

这篇关于绘图中的多个vlines给出错误ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆