R 修改每个面中的x轴标签

R 修改每个面中的x轴标签,r,ggplot2,R,Ggplot2,我有这个图表-我想在每个标签上添加文本N=xx,以表示观察次数。我知道如何做到这一点,我已经在没有方面的图表上做到了 当我在刻面图表上尝试时,它不起作用(我在所有3张图表的开放勾号上得到相同的N,在限制图表上得到相同的N,等等) 我希望有人能指出解决方案的方法,我如何控制给定方面的元素 library(ggplot2) library(scales) stat_sum_single <- function(fun, geom="point", ...) { stat_summary(

我有这个图表-我想在每个标签上添加文本
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
库(ggplot2)
图书馆(比例尺)

统计汇总单这并不完全是你想要做的,但我认为这是有帮助的(至少是一个好的开始)

库(ggplot2)
图书馆(plyr)

数据1对于相同的数据,我采用了四步方法

第一:对数据进行分组

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

open不清楚。是否要修改轴记号或刻面标签?轴记号标签-如果在第一个“打开”刻面上有6个观察结果,我希望它显示“打开(N=6)”,如果在第二个“打开”刻面上有9个,则其中一个将是“打开(N=9)”,感谢您的回答,我想为每个箱线图添加观察数showing@user1617979我完全改变了我的答案。谢谢,这实际上非常有效,我喜欢它,因为它允许使用facet_网格。通过在stat_summary中的aes()语句中添加y=0,我可以在底部对齐它们。我还学习了如何在ddply中使用我不熟悉的转换(将为我节省许多合并)
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="")
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)