有条件的ggplot中轴文本的条件格式 [英] Conditional formatting of axis text in faceted ggplot
问题描述
我试图根据几种研究(由X或Y表示)制作一个效果大小的森林图。数据集包含的行代表X型研究和Y型研究的总结统计,由 study ==Summary
。
$ (c(A,B,C,A,B,Summary,摘要))
mean <-c(1.29,0.76,2.43,1.68,1.22,1.7,0.76)
lowerCI <-c(0.84,0.50,1.58,1.1,0.8,1.11, 0.50)
upperCI < - c(1.95,1.16,3.67,2.54,1.85,2.56,1.16)
type < - as.factor(c(X,X,X ,Y,Y,X,Y))
df < - data.frame(study,mean,lowerCI,upperCI,type)
df $ study< - as.factor(df $ study)
我想粗体显示轴刻度标签为摘要效果大小。出于某种原因,当使用I子集时,使用 ifelse
语句不起作用。在此示例代码中,只有X类型的摘要标签以粗体显示,而两者都应以粗体显示。在我真实的,更复杂的代码中,两个摘要标签都没有加粗。
library(ggplot2)
plot < - ggplot(data = df,aes(y = study,x =意思是,xmin = lowerCI,xmax = upperCI,shape = type))+
geom_point(color ='black',size = 2)+
geom_errorbarh(height = .1)+
geom_point (data = subset(df,study =='Summary'),color ='black',size = 5)+
facet_grid(type〜。,scales ='free',space ='free')+
主题(axis.text.y = element_text(face = ifelse(levels(df $ study)==Summary,bold,plain)))
print(plot)
看起来罪魁祸首是 scales ='free'
代码,因为如果我放弃它或将其更改为 scales ='free_x'
,则摘要会粗体显示两次。但是,因为我想为一个子集中的研究删除多余的行(在本例中为研究C),所以我需要包含这些代码。
是否有解决方法? 使用 scale_y_discrete $ c>可以在给定刻度标签的每个实例上设置所有刻面的格式。 通过将所需的
标签
值(包括格式代码)分配给相应的 breaks
值:
ggplot(data = df,aes(y = study,x = mean,xmin = lowerCI,xmax = upperCI,shape = type)) +
geom_point(color ='black',size = 2)+
geom_errorbarh(height = .1)+
geom_point(data = subset(df,study =='Summary'), color ='black',size = 5)+
facet_grid(type〜。,scales ='free',space ='free')+
scale_y_discrete(breaks = levels(df $ study),
labels = c(levels(df $ study)[1:3],expression(bold(Summary))))
I'm attempting to make a forest plot of effect sizes from several studies, faceted by their type (X or Y). The dataset includes rows representing the summary statistics for studies of type X and studies of type Y, designated by study=="Summary"
.
study <- as.factor(c("A","B","C","A","B","Summary","Summary"))
mean <- c(1.29,0.76,2.43,1.68,1.22,1.7,0.76)
lowerCI <- c(0.84,0.50,1.58,1.1,0.8,1.11,0.50)
upperCI <- c(1.95,1.16,3.67,2.54,1.85,2.56,1.16)
type <- as.factor(c("X","X","X","Y","Y","X","Y"))
df <- data.frame(study, mean, lowerCI, upperCI, type)
df$study <- as.factor(df$study)
I want to bold the axis tick labels for the summary effect sizes. For some reason, using an ifelse
statement doesn't work when I subset. In this sample code, only the summary label for type X is bolded, whereas both should be bolded. In my real, more complex code, neither summary label is bolded.
library(ggplot2)
plot <- ggplot(data=df, aes(y=study, x=mean, xmin=lowerCI, xmax=upperCI, shape = type)) +
geom_point(color = 'black', size=2)+
geom_errorbarh(height=.1)+
geom_point(data=subset(df,study=='Summary'), color='black', size=5)+
facet_grid(type~., scales= 'free', space='free')+
theme(axis.text.y= element_text(face=ifelse(levels(df$study)=="Summary","bold","plain")))
print(plot)
It appears that the culprit is the scales='free'
code, because if I drop just that or change it to scales='free_x'
, summary is appropriately bolded twice. However, because I want to drop redundant rows for the studies that are in one subset but not the other (in this example, study C), I need this code to be included.
Is there a workaround?
You can format each instance of a given tick label across all facets using scale_y_discrete
by assigning the desired labels
value (including the formatting code) to a corresponding breaks
value:
ggplot(data=df, aes(y=study, x=mean, xmin=lowerCI, xmax=upperCI, shape = type)) +
geom_point(color = 'black', size=2)+
geom_errorbarh(height=.1)+
geom_point(data=subset(df,study=='Summary'), color='black', size=5)+
facet_grid(type~., scales= 'free', space='free') +
scale_y_discrete(breaks=levels(df$study),
labels=c(levels(df$study)[1:3], expression(bold("Summary"))))
这篇关于有条件的ggplot中轴文本的条件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!