多层geom_bar阴影图例

问题描述 投票:0回答:1

我有一个非常微不足道的问题,但由于某种原因我无法找到解决方案......

本质上,我有一个 stacked

geom_bar
图,我正在其中查看四个不同样本的插入和删除。我还比较了两种不同的方法,第二种方法为这两个变量提供了更高的检测值。

我能够生成一个图,在图例中显示不同变量的不同颜色;但是,我希望添加一个“阴影”图例,指示所使用的两种不同方法:分别为深色阴影中的reference和浅色阴影中的graph

有没有一种简单的方法可以做到这一点而不必诉诸分面? (我试图让情节尽可能简单)。提前致谢! 参见下文示例。 [![在此处输入图像描述][1]][1] 以及

code

library(grid)
library(ggh4x)
library(readxl)
library(scales)
library(ggdark)
library(ggpubr)
library(gtable)
library(plotly)
library(ggplot2)
library(ggrepel)
library(forcats)
library(reshape2)
library(ggchicklet) #round column
library(RColorBrewer)

excel_variants <- read_excel("/path/to/.xlsx")

df_variants <- data.frame(excel_variants)

df_variants$sample <- factor(df_variants$sample, levels=c('HG002', '6103671', '6103673', '6103675'))
df_variants$variant_type <- factor(df_variants$variant_type, levels=c('INS', 'DEL'))
df_variants$approach <- factor(df_variants$approach, levels=c('reference', 'graph'))

### PLOT THE DATA
plot <- 
  ggplot(df_variants, aes(x=sample, y=count, fill=variant_type)) + geom_bar(stat='identity', position='dodge', color='black', width=.3, alpha=.6) + scale_fill_manual(values=brewer.pal(12, "Paired")[c(7, 8)]) + theme_bw() +
  
  theme(plot.title=element_text(face='bold.italic', hjust=.5), legend.title=element_text(face='italic'), legend.position='bottom', legend.direction='horizontal') + 
  ggtitle("INDELs gain") + guides(fill=guide_legend(title='variant type', title.position='top', title.hjust=.5))
plot

ggsave(filename = "variants.png", width=6.5, height=5)
r ggplot2 legend geom-bar stacked-bar-chart
1个回答
0
投票

我实际上想出了一个可能的解决方案,尽管代码不是很优雅,我最终绘制了一个具有相应比例的新条形图(

ggnewscale
),然后在 y 轴归零。 见下文

还有

code

library(grid)
library(ggh4x)
library(readxl)
library(scales)
library(ggdark)
library(ggpubr)
library(gtable)
library(plotly)
library(ggplot2)
library(ggrepel)
library(forcats)
library(reshape2)
library(ggchicklet) #round column
library(ggnewscale)
library(RColorBrewer)

excel_variants <- read_excel("/path/to/.xlsx")

df_variants <- data.frame(excel_variants)

df_variants$sample <- factor(df_variants$sample, levels=c('HG002', '6103671', '6103673', '6103675'))
df_variants$variant_type <- factor(df_variants$variant_type, levels=c('INS', 'DEL'))
df_variants$approach <- factor(df_variants$approach, levels=c('reference', 'graph'))

### PLOT THE DATA
plot <- 
  ggplot(df_variants) +
  
  geom_bar(mapping=aes(x=sample, y=count, fill=variant_type), stat="identity", position="dodge", width=.3, alpha=.6) + 
  scale_fill_manual(values=brewer.pal(12, "Paired")[c(7, 8)], guide=guide_legend(title="variant type", ncol=2, title.position="top", title.hjust=.5)) + 
  
  new_scale_fill() +
  
  geom_bar(mapping=aes(x=sample, y=count*0, fill=approach), stat="identity", position="dodge", width=.3, alpha=.6) + 
  scale_fill_manual(values=brewer.pal(9, "Greys")[c(7, 3)], guide=guide_legend(title="approach", ncol=2, title.position="top", title.hjust=.5)) +
  
  theme_bw() + theme(plot.title=element_text(face='bold.italic', hjust=.5), legend.title=element_text(face='italic'), legend.position='bottom', legend.direction='horizontal') + 
  ggtitle("INDELs gain")
plot

ggsave(filename = "variants.png", width=6.5, height=5)
© www.soinside.com 2019 - 2024. All rights reserved.