如何在ggplot2中绘制治疗方法 [英] How to plot treatment means in ggplot2

查看:198
本文介绍了如何在ggplot2中绘制治疗方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里生成了一个随机的数据集,因为我无法发布自己的数据。这不是很好的数据,但它应该说明我正在努力做什么。



具体来说,我有三种治疗方法,每年都会对其效果进行测量。每次治疗每年重复6次。我想绘制治疗对年份(即在整个研究过程中)的因变量(DV)的影响,使用ggplot2



我试过了:

  ggplot(test,aes (x = factor(Year),y = DV,fill = factor(Grp)))+ 
geom_boxplot()

对于我生成的这些随机数据来说,这非常有效,但对于我的实际数据,这些框的大小变得更加可变,并且图很难解释。我想要做的是简化事情,而不是用一年的时间(而不是使用方框)绘制治疗方法,并为这些治疗手段添加标准误差。我也希望每年在每种治疗方法之间用直线加入这些治疗手段。有没有人知道这样做的方法?



在此先感谢!

解决方案

一种方法是在绘制新数据框之前重新计算平均值和sd值。另一种方法是定义自己的 stat _ 函数。这里是 stat_summary()帮助页面的修改示例。这将计算每年每次治疗的平均值和置信区间,并将其绘制为 geom =pointrange。行添加了 stat_summary() geom =line

  stat_sum_df < - 函数(fun,geom =crossbar,...){
stat_summary(fun.data = fun,geom = geom,width = 0.2,...)
}

ggplot(test,aes(x = factor(Year),y = DV,color = Grp,group = Grp))+
stat_sum_df(mean_cl_normal,geom =pointrange)+
stat_summary(fun.y =mean,geom =line)



更新



要获得标准错误,您必须创建新功能。我将它命名为 stat_mean_sd()。现在在 stat_summary()中使用这个函数。

  stat_mean_sd <-function (x){
cal.df< -data.frame(
y = mean(x),
ymin = mean(x)-sd(x)/ sqrt(length(x)) ,
ymax = mean(x)+ sd(x)/ sqrt(length(x)))
return(cal.df)
}

ggplot(测试,aes(x =因子(年),y = DV,color = Grp,group = Grp))+
stat_summary(fun.data = stat_mean_sd,geom =pointrange)+
stat_summary fun.y =mean,geom =line)


I have generated a random set of data here, as I cannot post my own. It's not great data but it should illustrate what I'm trying to do.

Specifically, I have three treatments, the effects of which are being measured each year. Each treatment is replicated 6 times each year.

I want to plot the effect of treatment on the Dependent variable (DV) with year (i.e. over the course of the study) using ggplot2

I have tried:

ggplot(test, aes(x = factor(Year), y = DV, fill = factor(Grp))) + 
geom_boxplot()

Which works well for this random data I have generated, but for my actual data the size of the boxes are much more variable and the graph is very difficult to interpret. What I wanted to do to simplify things, was to instead plot the treatment means with year (rather than using the boxes), and add standard errors to these treatment means. I also want to join up these treatment means with a straight line between each treatment's mean in each year. Does anyone know of a way to do this?

Thanks in advance!

解决方案

One way is to recalculate mean and sd values before plotting in new data frame. Another way would be to define own stat_ function. Here is modified example from stat_summary() help page. This will calculate mean and confidence interval for each treatment in each year and plot it as geom="pointrange". Lines are added with stat_summary() and geom="line".

stat_sum_df <- function(fun, geom="crossbar", ...) {
      stat_summary(fun.data=fun, geom=geom, width=0.2, ...)
  }

ggplot(test, aes(x = factor(Year), y = DV, colour=Grp,group=Grp)) + 
  stat_sum_df("mean_cl_normal",geom="pointrange")+
  stat_summary(fun.y="mean",geom="line")

Update

To get standard errors you have to make new function. I named it stat_mean_sd(). Now use this function inside stat_summary().

stat_mean_sd<-function(x){
  cal.df<-data.frame(
    y=mean(x),
    ymin=mean(x)-sd(x)/sqrt(length(x)),
    ymax=mean(x)+sd(x)/sqrt(length(x)))
  return(cal.df)
}

ggplot(test, aes(x = factor(Year), y = DV, colour=Grp,group=Grp)) + 
  stat_summary(fun.data=stat_mean_sd,geom="pointrange")+
  stat_summary(fun.y="mean",geom="line")

这篇关于如何在ggplot2中绘制治疗方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆