修改每个面的x轴标签 [英] Modify x-axis labels in each facet
问题描述
我有这张图表 - 我想在每个标签上添加文字 N = xx
来表示观察次数。我知道如何做到这一点,并且我已经在没有方面的图表上做过。
当我在平面图表上尝试时,它不起作用,(我得到了相同的N在所有3个图表上打开打勾,在受限制的N等等。)
我希望有人可以指出解决方案,我该如何控制在给定方面的元素?
library(ggplot2)
库(比例)
stat_sum_single< - 函数(fun,geom =point,...){
stat_summary(fun.y = fun,fill =red,geom = geom,size = 5,shape = 24) (1:3,100,替换= T),Physicians_Out = sample(1:3,100,替换= 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(开放,关闭))
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 = 0.2),color =blue)+ theme_bw ()()()
access_ch3 < - access_ch3 + theme(legend.position =none)+ scale_y_continuous(Gammagard Share,labels = percent)
gpo_labs5 < --\\\
,名称(表格(data1 $ Physicians_Out)),fixed = T),\\\
(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个元素,所以它也没有解决问题。 解决方案
你想这样做,但我认为这会有所帮助(至少是一个好的开始)
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ,变换,标签=长度(份额))
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)
I have this chart - I would like to add to each label the text N=xx
to denote the number of observations. I know how to do this and I have done that on charts with no facets.
When I tried that on the faceted chart it did not work, (I got the same N on the open tick on all 3 charts, the same N on the Restricted, etc.)
I hope someone can point the way to a solution, how do I control the elements on a given facet?
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
I tried creating the 9 labels and passing that vector to the scale_x_discrete
element, that just recycled the first 3, so it also did not solve the issue.
It is not exactly what you want to do , but I think this can be helpful ( at least a good start)
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)
这篇关于修改每个面的x轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!