首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘图中的多个vline给出了错误,ggplot2

绘图中的多个vline给出了错误,ggplot2
EN

Stack Overflow用户
提问于 2016-06-29 07:26:08
回答 1查看 2.1K关注 0票数 0

我想做的和here解释的完全一样,这个被接受的答案因为某些原因对我不起作用。

代码语言:javascript
复制
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“line”),以使绘图工作。但是,当添加vline时,我会得到以下错误:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-29 08:43:11

以下几点对我有用:

代码语言:javascript
复制
ggplot(tmp,aes(x, y, colour=category)) +
geom_line() +
geom_vline(data=tmp[c(13,26),],aes(xintercept=as.numeric(x)))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38092998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档