【发布时间】:2013-12-29 18:00:21
【问题描述】:
我有这个图表 - 我想在每个标签中添加文本 N=xx 以表示观察次数。我知道如何做到这一点,而且我已经在没有分面的图表上做到了。
当我在多面图表上尝试时,它不起作用,(我在所有 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
我尝试创建 9 个标签并将该向量传递给 scale_x_discrete 元素,它刚刚回收了前 3 个标签,所以它也没有解决问题。
【问题讨论】:
-
不清楚。是否要修改轴刻度或构面标签?
-
轴刻度标签 - 如果在 Open 的第一个方面有 6 个观察值,我希望它显示“Open (N=6)”,如果在第二个 Open 上有 9 个则为“打开 (N=9)"