【问题标题】:Multiple vlines in plot gives error, ggplot2图中的多个 vlines 给出错误,ggplot2
【发布时间】:2023-03-24 09:55:02
【问题描述】:

我想完全按照here 的解释做,由于某种原因,接受的答案对我不起作用。

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() 交换了 layers(geom="line") 以使绘图正常工作。但是,添加 vlines 时出现此错误:

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)

【问题讨论】:

    标签: r ggplot2 time-series


    【解决方案1】:

    以下对我有用:

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

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      相关资源
      最近更新 更多