You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用facet_wrap和ggarrange将Y轴标签置于方框中?

问题:将Y轴标签改为方框样式提升图表美观度

我是R语言新手,已经用ggplot和ggarrange生成了图表,希望把Y轴标签(HG002、HG005、peru60)改成像遗传标记类别(SNPs、INDELs)那样的方框样式,提升图表美观度。以下是我的代码和当前图表效果:

library(readxl)
library(scales)
library(ggdark)
library(ggpubr)
library(ggplot2)
library(forcats)
library(reshape2)
library(ggchicklet) #round column
library(RColorBrewer)

excel_HG002 <- read_excel("/media/mat/Extreme SSD/TheUniversityOfFerrara/2°Year/HG002-05_by_site_variants_R.xlsx") # compare variants call (SNVs and INDELs) in HG002
excel_HG005 <- read_excel("/media/mat/Extreme SSD/TheUniversityOfFerrara/2°Year/HG002-05_by_site_variants_R.xlsx", 2) # compare variants call (SNVs and INDELs) in HG002
excel_peru60 <- read_excel("/media/mat/Extreme SSD/TheUniversityOfFerrara/2°Year/HG002-05_by_site_variants_R.xlsx", 3) # compare variants call (SNVs and INDELs) in peru60

dfHG002 <- data.frame(excel_HG002) # for comparing samples genotype (HG002)
dfHG005 <- data.frame(excel_HG005) # for comparing samples genotype (HG005)
dfperu60 <- data.frame(excel_peru60) # for comparing samples genotype (peru60)

dfHG002$variant_type <- factor(dfHG002$variant_type, levels=c('SNPs', 'INDELs')) # order of variant types (HG002)
dfHG005$variant_type <- factor(dfHG005$variant_type, levels=c('SNPs', 'INDELs')) # order of variant types (HG005)
dfperu60$variant_type <- factor(dfperu60$variant_type, levels=c('SNPs', 'INDELs')) # order of variant types (peru60)

df2_HG002 <- with(dfHG002, dfHG002[order(variant_type, assembly, site_type),]) # collapse variants (HG002)
df2_HG005 <- with(dfHG005, dfHG005[order(variant_type, assembly, site_type),]) # collapse variants (HG005)
df2_peru60 <- with(dfperu60, dfperu60[order(variant_type, assembly, site_type),]) # collapse variants (peru60)

### PLOT variants call for HG002-05&peru60 on both assemblies
assessmentHG002 <- ggplot(dfHG002, aes(x=variant_type, y=value, fill=assembly)) + geom_col(position='stack', width=.2, alpha=.6, color='black')
ggplot(df2_HG002, aes(x=site_type, y=value, fill=assembly)) + geom_col(position='dodge', width=.2, alpha=.6, color='black') + 
  facet_wrap(~variant_type, scales='free', nrow=1) + scale_fill_manual(values=rev(brewer.pal(12, "Paired")[c(1:2)])) + labs(x=NULL, y='HG002') +
  guides(fill=guide_legend(title='assembly', title.position='top', title.hjust=.5, title.theme=element_text(face='italic'))) +
  theme_bw() + theme(strip.background=element_rect(fill='cornflowerblue')) -> test
  
assessmentHG005 <- ggplot(dfHG005, aes(x=variant_type, y=value, fill=assembly)) + geom_col(position='stack', width=.2, alpha=.6, color='black')
ggplot(df2_HG005, aes(x=site_type, y=value, fill=assembly)) + geom_col(position='dodge', width=.2, alpha=.6, color='black') + 
  facet_wrap(~variant_type, scales='free', nrow=1) + scale_fill_manual(values=rev(brewer.pal(12, "Paired")[c(5:6)])) + labs(x=NULL, y='HG005') +
  guides(fill=guide_legend(title='assembly', title.position='top', title.hjust=.5, title.theme=element_text(face='italic'))) +
  theme_bw() + theme(strip.background=element_rect(fill='lightcoral')) -> test2

assessmentPERU60 <- ggplot(dfperu60, aes(x=variant_type, y=value, fill=assembly)) + geom_col(position='stack', width=.2, alpha=.6, color='black')
ggplot(df2_peru60, aes(x=site_type, y=value, fill=assembly)) + geom_col(position='dodge', width=.2, alpha=.6, color='black') + 
  facet_wrap(~variant_type, scales='free', nrow=1) + scale_fill_manual(values=rev(brewer.pal(12, "Paired")[c(9:10)])) + labs(x=NULL, y='peru60') +
  guides(fill=guide_legend(title='assembly', title.position='top', title.hjust=.5, title.theme=element_text(face='italic'))) +
  theme_bw() + theme(strip.background=element_rect(fill='violet')) -> test3
  
figure <- ggarrange(test, test2, test3, nrow=3, common.legend=TRUE, legend='bottom')
figure + 
  theme(plot.title=element_text(face='bold.italic', hjust=.5)) + ggtitle("variant_calling — effect of assembly and sample's background")

当前图表效果

内容的提问来源于stack exchange,提问作者Matteo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 20:47:33