2016-12-16 83 views
0

我想中的R绘制一个非常简单的箱线图所示:ggplot:重新缩放轴线(日志)和切割轴

期望图形

enter image description here

它是一个对数链路(伽玛分布:jh_conc是激素浓度变量)用于分类分组变量的连续因变量(jh_conc)的广义线性模型(组:type of bee

我的脚本,我ALRE安以轩已经是:

> jh=read.csv("data_jh_titer.csv",header=T) 
> jh 
      group  jh_conc 
1   Queens 6.38542714 
2   Queens 11.22512563 
3   Queens 7.74472362 
4   Queens 11.56834171 
5   Queens 3.74020100 
6 Virgin Queens 0.06080402 
7 Virgin Queens 0.12663317 
8 Virgin Queens 0.08090452 
9 Virgin Queens 0.04422111 
10 Virgin Queens 0.14673367 
11  Workers 0.03417085 
12  Workers 0.02449749 
13  Workers 0.02927136 
14  Workers 0.01648241 
15  Workers 0.02150754 

fit1=glm(jh_conc~group,family=Gamma(link=log), data=jh) 

ggplot(fit, aes(group, jh_conc))+ 
     geom_boxplot(aes(fill=group))+ 
     coord_trans(y="log") 

产生的情节是这样的:

enter image description here

我的问题是:什么(GEOM)扩展,我可以使用拆分y轴和重新调整它们有什么不同?另外,如何根据对对数转换数据进行的posthoc测试,添加黑色圆圈(平均值;这些值是以对数刻度计算,然后回归为原始刻度的)水平线,这些水平线是显着性水平:**:p < 0.01 ,***:p < 0.001?

回答

0

无法通过设计在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