如何添加与 facet_grid 风格相似的标签
How to add labels in a similar style to 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"),size = 12, 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, size, 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
[我想重新创建这种标签}1
尝试以下添加“标题”
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"),size = 12, 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, size, 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, 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"),size = 12, replace = TRUE)
df <- data.frame(sex,stage,status,size)
df$stage_method<-"Stage method"
df$status_method<-"Status method"
graph_A <- ggplot(df, aes(status, size, fill=sex)) +
geom_boxplot() +
facet_grid(rows = vars(status_method), scales = "free")
graph_B <- ggplot(df, aes(stage, size, fill=sex)) +
geom_boxplot()+
facet_grid(rows = vars(stage_method), scales = "free")
figure_AB<-ggarrange(graph_A, graph_B, ncol=1, nrow=2,
labels=c("a", "b"),
common.legend = TRUE, legend = "top")
figure_AB
这一定是一个简单的解决方案,但我无法理解它。我有两种方法“阶段”(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"),size = 12, 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, size, 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
[我想重新创建这种标签}1
尝试以下添加“标题”
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"),size = 12, 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, size, 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, 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"),size = 12, replace = TRUE)
df <- data.frame(sex,stage,status,size)
df$stage_method<-"Stage method"
df$status_method<-"Status method"
graph_A <- ggplot(df, aes(status, size, fill=sex)) +
geom_boxplot() +
facet_grid(rows = vars(status_method), scales = "free")
graph_B <- ggplot(df, aes(stage, size, fill=sex)) +
geom_boxplot()+
facet_grid(rows = vars(stage_method), scales = "free")
figure_AB<-ggarrange(graph_A, graph_B, ncol=1, nrow=2,
labels=c("a", "b"),
common.legend = TRUE, legend = "top")
figure_AB