首页 文章

将配对行实现到boxplot.ggplot2中

提问于
浏览
3

我有一组配对数据,我正在使用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

    我不知道 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

    虽然我喜欢使用@ 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

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

    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

相关问题