如何解决如何以与 facet_grid 相似的样式添加标签
这一定是一个简单的解决方案,但我无法理解。我有两种方法“阶段”(1,2,3)和“状态”(pos,neg)和因素性别(女性,男性)。我为每种方法生成两个 Box_plots,然后使用 ggarrange 组合两个图。
首先,我想将每个图分别标记为“状态”和“阶段”。
我尝试使用 facet_grid 单独标记每个图,但它会拆分数据。
请找到附加的虚拟数据来说明我的问题。
非常感谢您考虑我的请求。
library(ggplot2)
size<-runif(12,min=20,max=40)
stage<-sample(0:3,12,replace=TRUE)
status<-sample(x = c("pos","neg"),size = 12,replace = TRUE)
sex<-sample(x = c("Female","Male"),replace = TRUE)
df <- data.frame(sex,stage,status,size)
#######Graph A##################
graph_A <- ggplot(df,aes(status,size,fill=sex)) +
geom_Boxplot()
# facet_grid(rows = vars(status),scales = "free")
#######Graph B##################
graph_B <- ggplot(df,aes(stage,fill=sex)) +
geom_Boxplot()
#facet_grid(rows = vars(status),scales = "free")
graph_B
#######Graph A&B##################
figure_AB<-ggarrange(graph_A,graph_B,ncol=1,nrow=2,labels=c("a","b"),common.legend = TRUE,legend = "top")
figure_AB
解决方法
尝试以下添加“标题”
size<-runif(12,min=20,max=40)
stage<-sample(0:3,12,replace=TRUE)
status<-sample(x = c("pos","neg"),size = 12,replace = TRUE)
sex<-sample(x = c("Female","Male"),replace = TRUE)
df <- data.frame(sex,stage,status,size)
#######Graph A##################
graph_A <- ggplot(df,aes(status,size,fill=sex)) +
geom_boxplot() +
#--------------- add a label layer,i.e. title here
labs(title = "status method")
#######Graph B##################
graph_B <- ggplot(df,aes(stage,fill=sex)) +
geom_boxplot() +
labs(title = "stage method")
#facet_grid(rows = vars(status),scales = "free")
graph_B
#######Graph A&B##################
figure_AB <- ggpubr::ggarrange(graph_A,graph_B,ncol=1,nrow=2,labels=c("a","b"),common.legend = TRUE,legend = "top")
figure_AB
,
不是一个非常“最佳”的解决方案。但是我设法通过向 df 添加两个变量来完成我想要的。
library(ggplot2)
library(ggpubr)
size<-runif(12,replace = TRUE)
df <- data.frame(sex,size)
df$stage_method<-"Stage method"
df$status_method<-"Status method"
graph_A <- ggplot(df,fill=sex)) +
geom_boxplot() +
facet_grid(rows = vars(status_method),scales = "free")
graph_B <- ggplot(df,fill=sex)) +
geom_boxplot()+
facet_grid(rows = vars(stage_method),scales = "free")
figure_AB<-ggarrange(graph_A,legend = "top")
figure_AB
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。