ggplot:rescale axis(log)和cut axis

时间:2016-12-16 21:48:22

标签: r ggplot2 axis boxplot

我想在R:

中绘制一个非常简单的箱形图

所需图表

enter image description here

它是一个对数链接(Gamma分布:jh_conc是一个激素浓度变量)一个连续因变量(jh_conc)的广义线性模型,用于分类分组变量(组:{{1 }})

我已经拥有的脚本是:

type of bee

结果情节如下:

enter image description here

我的问题是:我可以使用什么(geom)扩展来分割y轴并将它们重新缩放不同?另外,如何添加黑色圆圈(平均值;它们是在对数刻度上计算然后反向变换为原始刻度)水平线,这些水平线是基于对对数转换数据执行的后坐测试的显着性水平:**:p <0.01 ,***:p&lt; 0.001?

1 个答案:

答案 0 :(得分:1)

您无法通过设计在ggplot2中创建损坏的数字轴,主要是因为它在视觉上扭曲了所表示的数据/差异,并被视为具有误导性。

但是,您可以使用scale_log10() + annotation_logticks()来帮助压缩各种值的数据,或者更好地显示异方差数据。您还可以使用annotate来构建p值表示星和条。

您也可以使用它的命名属性轻松地从模型中获取信息,这里我们关注fit$coef

# make a zero intercept version for easy plotting
fit2 <- glm(jh_conc ~ 0 + group, family = Gamma(link = log), data = jh)
# extract relevant group means and use exp() to scale back
means <- data.frame(group = gsub("group", "",names(fit2$coef)), means = exp(fit2$coef))

ggplot(fit, aes(group, jh_conc)) +
    geom_boxplot(aes(fill=group)) +
    # plot the circles from the model extraction (means)
    geom_point(data = means, aes(y = means),size = 4, shape = 21, color = "black", fill = NA) +
    # use this instead of coord_trans
    scale_y_log10() + annotation_logticks(sides = "l") +
    # use annotate "segment" to draw the horizontal lines
    annotate("segment", x = 1, xend = 2, y = 15, yend = 15) +
    # use annotate "text" to add your pvalue *'s
    annotate("text", x = 1.5, y = 15.5, label = "**", size = 4) +
    annotate("segment", x = 1, xend = 3, y = 20, yend = 20) +
    annotate("text", x = 2, y = 20.5, label = "***", size = 4) +
    annotate("segment", x = 2, xend = 3, y = .2, yend = .2) +
    annotate("text", x = 2.5, y = .25, label = "**", size = 4) 

enter image description here