简体   繁体   English

ggplot2- geom_linerange与stat_smooth

[英]ggplot2- geom_linerange with stat_smooth

Oh wise ones: I've got a question about the use of geom_linerange(), attached is what I hope is a workable example to illustrate my problem. 噢,明智的选择:我对geom_linerange()的使用提出了疑问,我希望这是一个可行的示例来说明我的问题。

b=c(100,110,90,100,120,130,170,150,150,120,140,150,120,90,90,100,40,50,40,40,20,60,30)
test<-data.frame(a=c(2,2,2,4,4,4,4,6,6,6,6,6,6,8,8,8,10,10,10,10,10,10,10),
                 b=b,c=c(b-15))

testMelt <- melt(
  test, 
  id       = c("a"), 
  measured = c("b", "c")
  )


p <- ggplot(
  aes(
    x    = factor(a), 
    y    = value,
    fill= variable
    ),      
   data  = testMelt) + 
    geom_boxplot() + 
          stat_smooth(aes(group=variable,x=factor(a),y=value,fill=factor(variable)),data=testMelt)

My actual dataset is much larger, and the boxplots are a bit overwhelming. 我的实际数据集要大得多,而箱线图有点让人不知所措。 I think what I want is to use geom_linerange() somehow to show the range of the data, at "b" and "c", at each value of "a". 我想我想要以某种方式使用geom_linerange()来显示数据的范围,分别在“ b”和“ c”处,在每个“ a”值处。

The best I've come up with is: 我想出的最好的是:

p<- p+ geom_linerange(aes(as.factor(a),ymin=min(value),ymax=value,color=variable))

I can assume the "c" values are always equal to or less than "b", but if the range is smaller, this "covers it up". 我可以假设“ c”值始终等于或小于“ b”,但是如果范围较小,则“将其覆盖”。 Can I jitter the lines somehow? 我能以某种方式抖动线条吗? Is there a better solution? 有更好的解决方案吗?

In your geom_linerange call, add an additional argument position=position_dodge(width=0.3) . 在您的geom_linerange调用中,添加一个附加参数position=position_dodge(width=0.3) You can adjust the absolute width to change the separation between the vertical lines. 您可以调整绝对宽度以更改垂直线之间的间距。

在此处输入图片说明

My understanding of the question is that you want the line range to reflect the range for the combination a:b:c . 我对该问题的理解是,您希望行范围反映a:b:c组合的范围。

geom_linerange(aes(as.factor(a),ymin=min(value),ymax=value,color=variable)) will set the minimum value to the whole-dataset minimum (hence all the lines appear with the same minimum value. geom_linerange(aes(as.factor(a),ymin=min(value),ymax=value,color=variable))会将最小值设置为整个数据集的最小值(因此所有行都具有相同的最小值。

A couple of solutions. 几个解决方案。

Calculate the minima and maxima yourself 自己计算最小值和最大值

test_range <- ddply(testMelt, .(a,variable), summarize, 
                    val_min = min(value), val_max = max(value))

then run 然后跑

 ggplot(data  = testMelt) + 
    geom_boxplot(aes(x = factor(a), y = value, fill = variable)) + 
    stat_smooth(aes(group = variable, x = factor(a), y = value, 
                    fill = factor(variable))) +
     geom_linerange(data = test_range, aes(x = as.factor(a), ymin = val_min,
                    ymax = val_max, color = variable), 
                    position = position_dodge(width = 0.3))

Or, for an alternative to boxplots / line range use a violin plot. 或者,要替代箱线图/线范围,请使用小提琴图。

ggplot(data  = testMelt) + 
    geom_violin(aes(x = factor(a), y = value, fill = variable)) + 
    stat_smooth(aes(group = variable, x = factor(a), y = value, 
                    fill = factor(variable)))

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

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