Scientific Charts

Stacked Histogram 堆叠柱状图

以单细胞测序数据为例,探究各治疗组中各种细胞的比例。

library(ggplot2)

Cellratio <- prop.table(table(sce$cell_type, sce$trt_grp), margin = 2)
Cellratio <- as.data.frame(Cellratio)
Cellratio
colnames(Cellratio) <- c('cell_type', 'trt_group', 'Freq')
Cellratio$cell_type <- factor(Cellratio$cell_type)
Cellratio$trt_group <- factor(Cellratio$trt_group)

stacked_histogram <- ggplot(Cellratio) +
  geom_bar(
    aes(x = trt_group, y = Freq, fill = cell_type),
    stat = "identity",
    width = 0.7,
    size = 0.5,
    colour = '#222222'
  ) +
  theme_classic() +
  labs(x = 'Sample', y = 'Ratio') +
  theme(panel.border = element_rect(
    fill = NA,
    color = "black",
    linewidth = 0.5,
    linetype = "solid"
  )) +
  scale_fill_brewer(palette = "Spectral")

stacked_histogram

# save plot ----
if(FALSE) {
  ggplot2::ggsave(
    '~/stacked_histogram.pdf',
    width = 1200,
    height = 1000,
    units = c('px'),
    dpi = 300
  )
}