TransWikia.com

How to group already stacked bars when using R ggplot and geom_bar

Stack Overflow Asked by Christophe Antoniewski on December 30, 2020

I am using the following myggdf data frame in R

structure(list(Signature = c(1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 
5L, 1L, 3L, 5L), Sample = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L), .Label = c("F.vcf", "G.vcf", "H.vcf", 
"I.vcf"), class = "factor"), Contribution = c(638.885179211473, 
403.558378092796, 237.864729405206, 621.475592580731, 507.076811680132, 
844.111458945023, 340.773496994329, 693.189161464458, 868.648293203769, 
350.293155215537, 796.162462388531, 393.009724831982), label = c("lung", 
"lung", "lung", "lung", "lung", "lung", "ovary", "ovary", "ovary", 
"ovary", "ovary", "ovary")), row.names = c(NA, -12L), class = "data.frame")
   Signature Sample Contribution label
1          1  F.vcf     638.8852  lung
2          3  F.vcf     403.5584  lung
3          5  F.vcf     237.8647  lung
4          1  G.vcf     621.4756  lung
5          3  G.vcf     507.0768  lung
6          5  G.vcf     844.1115  lung
7          1  H.vcf     340.7735 ovary
8          3  H.vcf     693.1892 ovary
9          5  H.vcf     868.6483 ovary
10         1  I.vcf     350.2932 ovary
11         3  I.vcf     796.1625 ovary
12         5  I.vcf     393.0097 ovary

and would like to display the data as stacked bars (Contribution for each Signature) for each Sample, in addition grouped by a label (a tissue type).

Using the following code

ggplot(myggdf, aes(x=Sample, y=Contribution, fill=as.factor(Signature))) +
+     geom_bar(stat="identity", position='stack') + facet_grid(~label)

returns this plot that is not too far from what I am expecting.

However, how to get a sample shown only in its appropriate facet ?

Note that I am not that attached to the facet displaying. If there is a solution to cluster stacked bars by label in the same panel, it is all good.

Thanks for any tip

2 Answers

One option would be to add scales="free_x" to facet_grid:

library(ggplot2)

myggdf <- structure(list(Signature = c(1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 
                             5L, 1L, 3L, 5L), Sample = c("F.vcf", "F.vcf", "F.vcf", "G.vcf", 
                                                         "G.vcf", "G.vcf", "H.vcf", "H.vcf", "H.vcf", "I.vcf", "I.vcf", 
                                                         "I.vcf"), Contribution = c(638.8852, 403.5584, 237.8647, 621.4756, 
                                                                                    507.0768, 844.1115, 340.7735, 693.1892, 868.6483, 350.2932, 796.1625, 
                                                                                    393.0097), label = c("lung", "lung", "lung", "lung", "lung", 
                                                                                                         "lung", "ovary", "ovary", "ovary", "ovary", "ovary", "ovary")), row.names = c(NA, 
                                                                                                                                                                                       -12L), class = "data.frame")

ggplot(myggdf, aes(x=Sample, y=Contribution, fill=as.factor(Signature))) +
  geom_bar(stat="identity", position='stack') + facet_grid(~label, scales = "free_x")

Answered by stefan on December 30, 2020

Maybe one option would be using interaction() like this. Also you can enable the scales option inside facet_*() functions to reshape the axis as @stefan does. Here the code:

#Code
ggplot(myggdf, aes(x=interaction(Sample,label), y=Contribution, fill=as.factor(Signature))) +
  geom_bar(stat="identity", position='stack')

Output:

enter image description here

And an option with facets would imply creating new variables like these:

library(tidyverse)
#Data and plot
myggdf %>% 
  mutate(Var='var',Newlabel=paste0(Sample,'-',label)) %>%
  ggplot(aes(x=Var, y=Contribution, fill=as.factor(Signature))) +
  geom_bar(stat="identity", position='stack')+
  facet_wrap(.~Newlabel,scales='free')

Output:

enter image description here

And if only one row for plots is wanted, next code can be useful:

#Data and plot 2
myggdf %>% 
  mutate(Var='var',Newlabel=paste0(Sample,'-',label)) %>%
  ggplot(aes(x=Var, y=Contribution, fill=as.factor(Signature))) +
  geom_bar(stat="identity", position='stack')+
  facet_wrap(.~Newlabel,scales='free_x',nrow = 1)

Output:

enter image description here

Some data used:

#Data
myggdf <- structure(list(Signature = c(1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 
5L, 1L, 3L, 5L), Sample = c("F.vcf", "F.vcf", "F.vcf", "G.vcf", 
"G.vcf", "G.vcf", "H.vcf", "H.vcf", "H.vcf", "I.vcf", "I.vcf", 
"I.vcf"), Contribution = c(638.8852, 403.5584, 237.8647, 621.4756, 
507.0768, 844.1115, 340.7735, 693.1892, 868.6483, 350.2932, 796.1625, 
393.0097), label = c("lung", "lung", "lung", "lung", "lung", 
"lung", "ovary", "ovary", "ovary", "ovary", "ovary", "ovary")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))

Update: For a better difference in axis, it can be customized the labels like this:

#Data and plot 3
myggdf %>% 
  mutate(Newlabel=paste0(Sample,' (',label,')')) %>%
  ggplot(aes(x=Newlabel, y=Contribution, fill=as.factor(Signature))) +
  geom_bar(stat="identity", position='stack')+
  xlab('Sample-Tissue')

Output:

enter image description here

Answered by Duck on December 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP