简体   繁体   English

绘制 2x2x2 时间序列的原始值和预测值

[英]Plot raw and predict values for 2x2x2 time-series

This is the sample of my data这是我的数据样本

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)

Where, there are 2 factors for resources and fertilizer, so there are effectively 4 treatments and 4 x 4 = 16 replicates.其中,资源和肥料有 2 个因子,因此有效地有 4 个处理和 4 x 4 = 16 个重复。 Time is a factor with 10 levels.时间是一个有 10 个级别的因素。 I ran a model, and predicted values which is in the predict column.我运行了一个模型,并预测了predict列中的值。

Now I want to plot a time-series with time on the x-axis and mean of the fitted value (predict) on and the raw values (value) on the y-axis, for each type of resource and fertilizer (4 treatments) [That is 4 plots].现在我想为每种类型的资源和肥料(4 种处理)绘制一个时间序列,x 轴为时间,y 轴为拟合值(预测)的平均值和原始值(值) [即4个地块]。 I also want to add a confidence interval for the algal growth at each time point.我还想为每个时间点的藻类生长添加一个置信区间。 Here is my attempt at the code.这是我对代码的尝试。

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

With this simple code, I still get only 2 graphs and not 4. Also, the means of the predict function are not plotted.使用这个简单的代码,我仍然只得到 2 个图形而不是 4 个。此外,未绘制预测函数的均值。 I don't know how to plot the value and predicted together, and the corresponding confidence intervals.我不知道如何将valuepredicted一起绘制,以及相应的置信区间。

It would be helpful if anyone could also show how all four treatments can be on a single plot, and if I can get it to facet (like above)如果有人还可以展示所有四种处理方法如何在一个图上进行,并且我可以将其分面(如上),那将会很有帮助

My proposed solution is to create a second data.frame containing all summary statistics such as mean predicted value.我建议的解决方案是创建第二个 data.frame,其中包含所有汇总统计信息,例如平均预测值。 I show one way to do this with group_by and summarize from the dplyr package.我展示了一种使用group_by执行此操作的方法,并从dplyr包中进行了summarize The summary data needs to have columns resource , fertilizer and time that match the main data.汇总数据需要与主数据相匹配的列resourcefertilizertime The summary data also has columns with additional y values.汇总数据还包含带有附加y值的列。

Then, the main data and the summary data need to be provided separately to the appropriate ggplot functions, but not in the main ggplot() call.然后,需要将主要数据和汇总数据分别提供给适当的 ggplot 函数,而不是在主ggplot()调用中。 facet_grid can be used to split the data into four plots. 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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM