2016-08-17 40 views
3

我正試圖根據處理條件和訪問次數來計算一些數字結果的箱型圖,每個箱子中的觀察數量都放置在該圖下,並且訪問次數也標記爲。以下是一些可用於說明的虛假數據,我舉了兩個我嘗試過的方法,但這些方法並不適用。在多面圖中用N註釋x軸

library(ggplot2) 
library(plyr) 

trt  <- factor(rep(LETTERS[1:2],150),ordered=TRUE) 
vis  <- factor(c(rep(1,150),rep(2,100),rep(3,50)),ordered=TRUE) 
val  <- rnorm(300) 
data  <- data.frame(trt,vis,val) 
data.sum <- ddply(data, .(vis, trt), summarise, 
      N=length(na.omit(val))) 
mytheme <- theme_bw() + theme(panel.margin = unit(0, "lines"), strip.background = element_blank()) 

下面的代碼會生成一個在我想要它們的地方有N個標籤的圖。它通過從我創建的輔助數據集中獲取摘要數據來完成此操作。但是,我無法弄清楚如何在x軸上標記訪問(理想情況下,在各個盒子標籤下面),或者以其他方式(例如將它們分隔爲面板的線條)在視覺上描繪訪問。

plot1 <- ggplot(data) + 
      geom_boxplot(aes(x=vis:trt,y=val,group=vis:trt,colour=trt), show.legend=FALSE) + 
      scale_x_discrete(labels=paste(data.sum$trt,data.sum$N,sep="\n")) + 
      labs(x="Visit") + mytheme 

下圖是接近我想要比上面的一個,因爲它具有治療和參觀一個不錯的層次,和一個漂亮的格式劃定訪問。但是,對於每個面板,它會從摘要數據中第一行抓取與處理條件相匹配的N,因爲它不會「知道」每個面需要使用與該訪問對應的行。

plot2 <- ggplot(data) +  geom_boxplot(aes(x=trt,y=val,group=trt,colour=trt), show.legend=FALSE) + 
      facet_wrap(~ vis, drop=FALSE, switch="x", nrow=1) + 
      scale_x_discrete(labels=paste(data.sum$trt,data.sum$N,sep="\n")) + 
      labs(x="Visit") + mytheme 

回答

2

一個解決辦法是處理你的數據集,以便您的x變量是trtN之間的相互作用。

取消您已有的功能,您可以通過mergeN添加到原始數據集中。

test = merge(data, data.sum) 

然後,讓一個新的變量是trtN組合。

test = transform(test, trt2 = paste(trt, N, sep = "\n")) 

現在做的情節,使用在X軸上的新trt2變量和使用facet_wrapscales = "free_x",讓每方面的不同標籤。

ggplot(test) +  
    geom_boxplot(aes(x = trt2, y = val, group = trt, colour = trt), show.legend = FALSE) + 
    facet_wrap(~ vis, drop = FALSE, switch="x", nrow = 1, scales = "free_x") + 
    labs(x="Visit") + 
    mytheme 

enter image description here

+0

這引起了我大部分的方式存在。不幸的是,'scales =「free_x」似乎與'drop = FALSE'打架。 在我的大範圍內,我使用grid.arrange創建一個包含訪問1變化的第二個陰謀圖,並且我想安排Plot 2以便(Vis2-Vis1)在Plot中與Vis2可視地對齊1。它工作正常,沒有我的標籤感謝drop = FALSE,但是當我添加scales =「free_x」時失敗,出現以下錯誤消息: '「if(zero_range(from)|| zero_range(to)缺少需要TRUE/FALSE的值「' 我還沒有想出是否有解決方法。 – ErinMcJ

+0

@ErinMcJ你有沒有例子?您可能會以您的實際情況爲例提出一個新問題。這可能是因爲樣本大小在標註內部進行註釋,或者避免使用分面,並且使用gridExtra類型的解決方案最終會爲您更好地工作。 – aosmith

+0

謝謝。這是更復雜的後續問題。 http://stackoverflow.com/questions/39027770/annotating-x-axis-with-n-in-faceted-plot-but-preserve-empty-facets – ErinMcJ

1

因爲這個功能不是在一個良好的內置解決辦法是grid.extra

library(gridExtra) 
p1 <- ggplot(data[data$vis==1,]) +  geom_boxplot(aes(x=trt,y=val,group=trt,colour=trt), show.legend=FALSE) + 
    #facet_wrap(~ vis, drop=FALSE, switch="x", nrow=1) + 
    scale_x_discrete(labels=lb[1:2]) + #paste(data.sum$trt,data.sum$N,sep="\n") 
    labs(x="Visit") + mytheme 

p2 <- ggplot(data[data$vis==2,]) +  geom_boxplot(aes(x=trt,y=val,group=trt,colour=trt), show.legend=FALSE) + 
    #facet_wrap(~ vis, drop=FALSE, switch="x", nrow=1) + 
    scale_x_discrete(labels=lb[3:4]) + #paste(data.sum$trt,data.sum$N,sep="\n") 
    labs(x="Visit") + mytheme 

p3 <- ggplot(data[data$vis==3,]) +  geom_boxplot(aes(x=trt,y=val,group=trt,colour=trt), show.legend=FALSE) + 
    #facet_wrap(~ vis, drop=FALSE, switch="x", nrow=1) + 
    scale_x_discrete(labels=lb[5:6]) + #paste(data.sum$trt,data.sum$N,sep="\n") 
    labs(x="Visit") + mytheme 


grid.arrange(p1,p2,p3,nrow=1,ncol=3) # fully customizable 

enter image description here

相關: Varying axis labels formatter per facet in ggplot/R

你也可以讓他們垂直或做其他的轉換:

enter image description here