2013-12-11 10 views
6

私はこのチャートを持っています - 各ラベルに、観察数を表すテキストN=xxを追加したいと思います。私はこれを行う方法を知っており、私は面がないチャートでこれを行っています。私はそれが動作しませんでした多面的なチャート上で、という試みた各ファセットのx軸ラベルを変更します

(私はすべての3つのチャート上でオープンダニに同じNを得た、制限付きの同じ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 

私はそれはまた、問題が解決しなかったので、ちょうど最初の3をリサイクルすることを、9枚のラベルを作成し、scale_x_discrete要素にそのベクトルを渡してみました。同じデータを持つ

+1

ですが明確ではありません。軸ティックやファセットラベルを変更しますか? – agstudy

+0

軸の目盛りのラベル - 最初のファセットでOpenの場合は6つの観測があり、2つ目のOpenで9が開いている(N = 9)場合は「Open(N = 6) – 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でtransformを使う方法を学びました(私に多くのマージを救うでしょう) – user1617979

7

は、私は4段階のアプローチを踏襲しました。

まず:データを

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="") 

サード:第2 &するY軸のラベル&テキストを除去するためのテーマを作成します第3のサブグラフ

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

フィン味方:次のような結果与えグラフ

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

関連する問題