在带有日期的ggplot2中添加多行

时间:2018-10-09 20:44:51

标签: r ggplot2 time-series

在这个问题上苦苦挣扎了一段时间,不确定是怎么回事。我知道这个问题一定会证明我对ggplot的不足。

我有一个这样的脚本,可以很好地运行:

beta.bray= c(0.681963714,0.73301985,0.6797153,0.79358052,0.85055556,0.76297686,0.60653007)

beta.bray.gradient=c(0.182243513, 0.565267411,0.427449441,0.655012391,0.357146893,0.286457524,0.338706138)

Date=c("07/18/14","07/26/14","08/19/14","08/25/14", "07/25/15","08/22/15", "07/26/16")
dat=data.frame(Date, beta.bray, beta.bray.gradient)

test<-ggplot(dat, aes(x=reorder(Date, x=fct_inorder(Date)), y=beta.bray, group=1))+geom_line(linetype="dashed")+geom_point()+
  labs(x="Date", y="β, multiple-site dissimilarity", title="SNARL riffle site/site β through time, 2014-2016") +coord_cartesian(xlim=c(1,7),ylim=c(.58,.85))

test

但是当我想为beta.bray.gradient添加另一行时,我什么都无法工作。我认为这与我在上面的代码中使用aes()的方式有关,但是我不知道该怎么做,以便使用reorder()和fct_inorder()来确保绘制日期以正确的方式。这是我尝试添加第二行的方法的示例:

    test<-ggplot(dat, aes(x=reorder(Date, x=fct_inorder(Date)), y=beta.bray, group=1))+geom_line(linetype="dashed")+geom_point()+
  geom_line(dat, aes(y=beta.bray.gradient, linetype="c"))+
  labs(x="Date", y="β, multiple-site dissimilarity", title="SNARL riffle site/site β through time, 2014-2016") +coord_cartesian(xlim=c(1,7),ylim=c(.58,.85))

在这些情况下,我们会看到许多错误,在这种情况下,Error: ggplot2 doesn't know how to deal with data of class uneval

1 个答案:

答案 0 :(得分:1)

我认为最好为x轴使用实际的日期对象并将数据重整为长格式:

onClick