2013-12-11 39 views
6

我有這個圖表 - 我想添加到每個標籤的文本N=xx來表示觀察的數量。我知道如何做到這一點,並且我已經在沒有方面的圖表上做過。修改每個方面的x軸標籤

當我嘗試了多面的圖表沒有工作的,(我得到了相同的N上開蜱在所有3個圖表,相同的N上的限制等)

我希望有人能指出解決方案的方式,我如何控制給定方面的元素?

library(ggplot2) 
library(scales) 

stat_sum_single <- function(fun, geom="point", ...) { 
    stat_summary(fun.y=fun, fill="red", geom=geom, size = 5, shape=24) 
} 

set.seed(1) 
data1 <- data.frame(Physicians_In=sample(1:3,100,replace=T),Physicians_Out=sample(1:3,100,replace=T),share=runif(100,0,1)) 
data1$Physicians_In <- factor(data1$Physicians_In,levels=c(1,2,3),labels=c("Open","Restricted","Closed")) 
data1$Physicians_Out <- factor(data1$Physicians_Out,levels=c(1,2,3),labels=c("Open","Restricted","Closed")) 

access_ch3 <- ggplot(data1,aes(x=Physicians_In,y=share,fill=Physicians_In))+geom_boxplot()+stat_sum_single(mean) 
access_ch3 <- access_ch3 +geom_jitter(position = position_jitter(width = .2),color="blue")+theme_bw() 
access_ch3 <- access_ch3 + theme(legend.position="none") +scale_y_continuous("Gammagard Share",labels=percent) 
gpo_labs5 <- paste(gsub("/","-\n",names(table(data1$Physicians_Out)),fixed=T),"\n(N=",table(data1$Physicians_Out),")",sep="") 
access_ch3 <- access_ch3 + scale_x_discrete("Physician Access (In Hospital)",labels=gpo_labs5) 
access_ch3 <- access_ch3 +facet_grid(.~Physicians_Out,labeller=label_both) 
access_ch3 

我試圖創建9個標籤和傳遞一個向量的scale_x_discrete元素,只是回收第3,所以它也沒有解決問題。

+1

是不明確的。你想修改軸刻度或刻面標籤嗎? – agstudy

+0

軸刻度標籤 - 如果在Open的第一個面上有6個觀察結果,我希望它顯示「Open(N = 6)」,如果在第二個Open上有9個觀察結果,那麼它將是「開放的(N = 9)」 – user1617979

回答

7

這不正是你想做的事,但我認爲這可能是有益的(至少一個良好的開端)

library(ggplot2) 
library(plyr) 
data1 <- ddply(data1,.(Physicians_Out,Physicians_In),transform,label = length(share)) 
ggplot(data1,aes(x=Physicians_In,y=share,fill=Physicians_In))+ 
    geom_boxplot() + 
    stat_sum_single(mean) + 
    facet_grid(.~Physicians_Out,labeller=label_both,scales='free_x') + 
    stat_summary(fun.y=min,aes(label=paste0('N=',label)),geom='text',col='blue',cex=5) 

enter image description here

+0

感謝您的回答,我想爲每個展示的箱子添加觀察次數 – user1617979

+1

@ user1617979我完全改變了我的答案。 – agstudy

+1

謝謝,這實際上效果很好,我喜歡它,因爲它允許使用facet_grid。我可以通過在stat_summary的aes()語句中添加y = 0來在底部對齊它們。我還學會了如何在ddply中使用我不熟悉的變換(將爲我節省很多合併) – user1617979

7

使用相同的數據我也跟着四分步法。

第一:子集劃分的數據

open <- subset(data1, Physicians_Out == "Open") 
restr <- subset(data1, Physicians_Out == "Restricted") 
closed <- subset(data1, Physicians_Out == "Closed") 

第二:創建用於不同子集

標籤
labs.open <- paste(gsub("/","-\n",names(table(open$Physicians_In)),fixed=T), 
       "\n(N=",table(open$Physicians_In),")",sep="") 
labs.restr <- paste(gsub("/","-\n",names(table(restr$Physicians_In)),fixed=T), 
       "\n(N=",table(restr$Physicians_In),")",sep="") 
labs.closed <- paste(gsub("/","-\n",names(table(closed$Physicians_In)),fixed=T), 
       "\n(N=",table(closed$Physicians_In),")",sep="") 

第三:用於除去y軸創建主題標籤&文本的第二&第3個子圖

mytheme <- theme(
    axis.title.y = element_blank(), 
    axis.text.y = element_blank(), 
    axis.ticks.y = element_blank() 
) 

Fin盟友:創建圖形

p1 <- ggplot(open,aes(x=Physicians_In,y=share,fill=Physicians_In)) + 
    geom_boxplot() + stat_sum_single(mean) + 
    geom_jitter(position = position_jitter(width = .2),color="blue") + 
    guides(fill=FALSE) + 
    ggtitle(paste("Physician Access (Out): Open\nN = (", nrow(open), ")\n")) + 
    scale_y_continuous("Gammagard Share",labels=percent) + 
    scale_x_discrete("\nPhysician Access (In Hospital)",labels=labs.open) + 
    theme_bw() 

p2 <- ggplot(restr,aes(x=Physicians_In,y=share,fill=Physicians_In)) + 
    geom_boxplot() + stat_sum_single(mean) + 
    geom_jitter(position = position_jitter(width = .2),color="blue") + 
    guides(fill=FALSE) + 
    ggtitle(paste("Physician Access (Out): Restricted\nN = (", nrow(restr), ")\n")) + 
    scale_x_discrete("\nPhysician Access (In Hospital)",labels=labs.restr) + 
    theme_bw() + mytheme 

p3 <- ggplot(closed,aes(x=Physicians_In,y=share,fill=Physicians_In)) + 
    geom_boxplot() + stat_sum_single(mean) + 
    geom_jitter(position = position_jitter(width = .2),color="blue") + 
    guides(fill=FALSE) + 
    ggtitle(paste("Physician Access (Out): Closed\nN = (", nrow(closed), ")\n")) + 
    scale_x_discrete("\nPhysician Access (In Hospital)",labels=labs.closed) + 
    theme_bw() + mytheme 

library(gridExtra) 

grid.arrange(p1, p2, p3, ncol=3) 

這樣做具有以下結果:

enter image description here