将配对行实现到boxplot.ggplot2中

时间:2018-03-19 19:15:04

标签: r ggplot2 boxplot

我有一组配对数据,我正在使用ggplot2.boxplot(easyGgplot2包)和添加(抖动)的单个数据点:

ggplot2.boxplot(data=INdata,xName='condition',yName='vicarious_pain',groupName='condition',showLegend=FALSE,
  position="dodge",
  addDot=TRUE,dotSize=3,dotPosition=c("jitter", "jitter"),jitter=0.2,
  ylim=c(0,100),
  backgroundColor="white",xtitle="",ytitle="Pain intenstity",mainTitle="Pain intensity",
  brewerPalette="Paired")

INDATA:

ID,condition,pain
1,Treatment,4.5
3,Treatment,12.5
4,Treatment,16
5,Treatment,61.75
6,Treatment,23.25
7,Treatment,5.75
8,Treatment,5.75
9,Treatment,5.75
10,Treatment,44.5
11,Treatment,7.25
12,Treatment,40.75
13,Treatment,17.25
14,Treatment,2.75
15,Treatment,15.5
16,Treatment,15
17,Treatment,25.75
18,Treatment,17
19,Treatment,26.5
20,Treatment,27
21,Treatment,37.75
22,Treatment,26.5
23,Treatment,15.5
25,Treatment,1.25
26,Treatment,5.75
27,Treatment,25
29,Treatment,7.5
1,No Treatment,34.5
3,No Treatment,46.5
4,No Treatment,34.5
5,No Treatment,34
6,No Treatment,65
7,No Treatment,35.5
8,No Treatment,48.5
9,No Treatment,35.5
10,No Treatment,54.5
11,No Treatment,7
12,No Treatment,39.5
13,No Treatment,23
14,No Treatment,11
15,No Treatment,34
16,No Treatment,15
17,No Treatment,43.5
18,No Treatment,39.5
19,No Treatment,73.5
20,No Treatment,28
21,No Treatment,12
22,No Treatment,30.5
23,No Treatment,33.5
25,No Treatment,20.5
26,No Treatment,14
27,No Treatment,49.5
29,No Treatment,7

结果情节如下所示:

enter image description here

然而,由于这是配对数据,我想在图中表示这一点 - 特别是在配对数据点之间添加线。我试过添加

... + geom_line(aes(group = ID))

..但我无法将此实现到ggplot2.boxplot代码中。相反,我得到了这个错误:

  

if(addMean)中的错误p< - p + stat_summary(fun.y = mean,geom =“point”,:     论证不能被解释为合乎逻辑的   另外:警告信息:   在if(addMean)中,p< - p + stat_summary(fun.y = mean,geom =“point”,:     条件的长度> 1,只使用第一个元素

感谢任何有关此事的意见!

3 个答案:

答案 0 :(得分:2)

我不知道ggplot2.boxplot来自的包,但我会告诉你如何在ggplot中执行请求的操作。

ggplot请求的输出有点问题,因为您希望连接它们的点和线都抖动相同的量。执行此操作的一种方法是在绘制图之前抖动点。但是x轴是离散的,这是一个解决方法:

b <- runif(nrow(df), -0.1, 0.1)

ggplot(df) +
  geom_boxplot(aes(x = as.numeric(condition), y = pain, group = condition))+
  geom_point(aes(x = as.numeric(condition) + b, y = pain)) +
  geom_line(aes(x  = as.numeric(condition) + b, y = pain, group = ID)) +
  scale_x_continuous(breaks = c(1,2), labels = c("No Treatment", "Treatment"))+
  xlab("condition")

enter image description here

首先,我通过调用b制作了一个抖动向量,并将x轴转换为数字,因此我可以将b添加到x轴坐标。后者我重新标记了x轴。

我同意eipi10的评论,即情节更好,没有抖动:

ggplot(df, aes(condition, pain)) +
  geom_boxplot(width=0.3, size=1.5, fatten=1.5, colour="grey70") +
  geom_point(colour="red", size=2, alpha=0.5) +
  geom_line(aes(group=ID), colour="red", linetype="11") +
  theme_classic()

enter image description here

和更新的情节与抖动点eipi10风格:

ggplot(df) +
  geom_boxplot(aes(x = as.numeric(condition),
                   y = pain,
                   group = condition),
               width=0.3,
               size=1.5,
               fatten=1.5,
               colour="grey70")+
  geom_point(aes(x = as.numeric(condition) + b,
                 y = pain),
             colour="red",
             size=2,
             alpha=0.5) +
  geom_line(aes(x  = as.numeric(condition) + b,
                y = pain,
                group = ID),
            colour="red",
            linetype="11") +
  scale_x_continuous(breaks = c(1,2),
                     labels = c("No Treatment", "Treatment"),
                     expand = c(0.2,0.2))+
  xlab("condition") +
  theme_classic()

enter image description here

答案 1 :(得分:1)

虽然我喜欢使用@ missuse的回答显示的gsplot的oldschool绘图方式,但我想检查是否也可以使用基于ggplot2.boxplot的代码。

我加载了您的数据:

'data.frame':   52 obs. of  3 variables:
 $ ID       : int  1 3 4 5 6 7 8 9 10 11 ...
 $ condition: Factor w/ 2 levels "No Treatment",..: 2 2 2 2 2 2 2 2 2 2 ...
 $ pain     : num  4.5 12.5 16 61.8 23.2 ...

并调用您的代码,在您建议自己的时候在最后添加geom_line:

ggplot2.boxplot(data = INdata,xName = 'condition', yName = 'pain', groupName = 'condition',showLegend = FALSE,
                position = "dodge",
                addDot = TRUE, dotSize = 3, dotPosition = c("jitter", "jitter"), jitter = 0,
                ylim = c(0,100),
                backgroundColor = "white",xtitle = "",ytitle = "Pain intenstity", mainTitle = "Pain intensity",
                brewerPalette = "Paired") + geom_line(aes(group = ID))

请注意,我将抖动设置为0.结果图如下所示: R plot

如果没有将抖动设置为0,则线条仍然从每个箱线图的中间运行,忽略点的水平位置。

不确定您的通话为什么会出错。我认为这可能是一个因素问题,但我发现我的ID变量不是因子类。

答案 2 :(得分:1)

我在ggplot2.boxplot方法中实现了missuse的抖动解决方案,以便对齐点和线。而不是使用&#34; addDot&#34;,我不得不使用geom_point(以及使用geom_line的行)添加点,所以我可以将相同的抖动向量应用于点和线。

b <- runif(nrow(df), -0.2, 0.2)

ggplot2.boxplot(data=df,xName='condition',yName='pain',groupName='condition',showLegend=FALSE,
      ylim=c(0,100),
      backgroundColor="white",xtitle="",ytitle="Pain intenstity",mainTitle="Pain intensity",
      brewerPalette="Paired") +
      geom_point(aes(x=as.numeric(condition) + b, y=pain),colour="black",size=3, alpha=0.7) +
      geom_line(aes(x=as.numeric(condition) + b, y=pain, group=ID), colour="grey30", linetype="11", alpha=0.7)

enter image description here