2016-12-16 73 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