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

R 绘制2x2x2时间序列的原始值和预测值,r,ggplot2,time-series,factorial,R,Ggplot2,Time Series,Factorial,这是我的数据样本 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", "nitro

这是我的数据样本

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个。此外,未绘制预测函数的平均值。我不知道如何将
预测值
以及相应的置信区间一起绘制


如果有人也能在一个图上展示所有四种处理方法的效果,如果我能将其应用到切面(如上所述)

我建议的解决方案是创建第二个data.frame,其中包含所有汇总统计数据,如平均预测值。我展示了一种通过
dplyr
包中的
groupby
summary
实现这一点的方法。摘要数据需要有与主数据匹配的列
资源
肥料
时间
。摘要数据也有附加
y
值的列

然后,主数据和摘要数据需要分别提供给相应的ggplot函数,但不能在main
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)

在进行否决投票之前,请告诉我问题出在哪里?不是否决投票人,而是一个你希望它看起来像什么的例子会有所帮助。即使是手绘的,也只是为了让回答者确信他们在帮忙。我已经尽力解释了。这有帮助吗?我稍微编辑了您的代码,将
data$nutrition
更改为
data$resource
。如果不正确,请重新更改或拒绝编辑。谢谢。这看起来不错,如果我不想用肥料来刻面,而是用不同的颜色来着色,我怎么能做到这一点呢?现在,在两个geompoints函数中的aes之后,我添加了color=infection added。无论哪里有西红柿,我都添加了c(“西红柿”,“蓝色”),并更改了facet_网格(参考资料~),但我在绘图上没有任何区别。facet_网格一直作为错误弹出在示例数据中没有名为
infection
的列。此外,为因子级别指定颜色与ggplot中的工作方式不同;我使用“西红柿”的例子是设置静态颜色。关于使用颜色而不是刻面来区分不同等级的肥料的问题,请查看这是否有助于开始:
ggplot()+geom_point(data=data.melt,aes(x=time,y=value,group=fulture),position=position_dodge(width=0.5))+geom_errorbar(数据=汇总数据,aes(x=时间,ymax=上限ci,ymin=下限ci,组=肥料,颜色=肥料),位置=位置减淡(宽度=0.5),宽度=0.5)+几何点(数据=汇总数据,aes(x=时间,y=预测平均值,组=肥料,颜色=肥料),大小=3,位置=位置减淡(宽度=0.5)+面网格(资源~)
(1)很抱歉,评论(2)中的拼写错误帮了大忙。谢谢!
# 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)