首页 文章

如何在R中绘制2x2x2时间序列的原始值和预测值?

提问于
浏览
2

这是我的数据样本

library(tidyr)
library(dplyr)
library(ggplot2)

resource <- c("good","good","bad","bad","good","good","bad","bad","good","good","bad","bad","good","good","bad","bad")

fertilizer <- c("none", "nitrogen","none","nitrogen","none", "nitrogen","none","nitrogen","none", "nitrogen","none","nitrogen","none", "nitrogen","none","nitrogen")

t0 <-  sample(1:20, 16)
t1 <-  sample(1:20, 16) 
t2 <-  sample(1:20, 16)
t3 <-  sample(1:20, 16)
t4 <-  sample(1:20, 16)
t5 <-  sample(1:20, 16)
t6 <-  sample(10:100, 16)
t7 <-  sample(10:100, 16)
t8 <-  sample(10:100, 16)
t9 <-  sample(10:100, 16)
t10 <-  sample(10:100, 16)

replicates <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)

data <- data.frame(resource, fertilizer,replicates, t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10)

data$resource <- as.factor(data$resource)
data$fertilizer <- as.factor(data$fertilizer)

data.melt <- data %>% ungroup %>% gather(time, value, -replicates, -resource, -fertilizer)

data.melt$predict <- sample(1:200, 176)

其中,资源和肥料有2个因素,因此有效处理4次,4次4 = 16次重复 . 时间是10个级别的因素 . 我运行了一个模型,并预测了 predict 列中的值 .

现在我想在x轴上绘制一个时间序列,在每个类型的资源和肥料上绘制拟合值(预测值)的平均值和y轴上的原始值(值)(4个处理) [这是4个地块] . 我还想在每个时间点添加藻类生长的置信区间 . 这是我对代码的尝试 .

ggplot(df, aes(x=time, y=predicted)) + geom_point(size=3)+ stat_summary(geom = "point", fun.y = "mean") + facet_grid(resource + fertilizer ~.)

使用这个简单的代码,我仍然只得到2个图而不是4.而且,没有绘制预测函数的平均值 . 我不知道如何将 valuepredicted 一起绘制,以及相应的置信区间 .

如果有人也可以在单个情节中展示所有四种治疗方法,并且如果我可以将其解决(如上所述),那将会很有帮助

1 回答

  • 3

    我提出的解决方案是创建第二个data.frame,其中包含所有汇总统计信息,例如平均预测值 . 我通过 dplyr 包中的 group_bysummarize 显示了一种方法 . 摘要数据需要包含与主数据匹配的列 resourcefertilizertime . 摘要数据还包含具有其他 y 值的列 .

    然后,主数据和摘要数据需要单独提供给适当的ggplot函数,但不能在主 ggplot() 调用中提供 . facet_grid 可用于将数据拆分为四个图 .

    # Convert time to factor, specifying correct order of time points.
    data.melt$time = factor(data.melt$time, levels=paste("t", seq(0, 10), sep=""))
    
    # Create an auxilliary data.frame containing summary data.
    # I've used standard deviation as place-holder for confidence intervals;
    # I'll let you calculate those on your own.
    summary_dat = data.melt %>%
                  group_by(resource, fertilizer, time) %>%
                  summarise(mean_predicted=mean(predict),
                            upper_ci=mean(predict) + sd(predict),
                            lower_ci=mean(predict) - sd(predict))
    
    p = ggplot() + 
        theme_bw() +
        geom_errorbar(data=summary_dat, aes(x=time, ymax=upper_ci, ymin=lower_ci),
                      width=0.3, size=0.7, colour="tomato") + 
        geom_point(data=data.melt, aes(x=time, y=value),
                   size=1.6, colour="grey20", alpha=0.5) +
        geom_point(data=summary_dat, aes(x=time, y=mean_predicted),
                   size=3, shape=21, fill="tomato", colour="grey20") +
        facet_grid(resource ~ fertilizer)
    
    ggsave("plot.png", plot=p, height=4, width=6.5, units="in", dpi=150)
    

    enter image description here

相关问题