Home > Software design >  barplot ggplot with bars filled by values of another df
barplot ggplot with bars filled by values of another df

Time:02-04

Hi would to create a barplot like this enter image description here

but the bars should by filled by the values of this plot, leaving the rest in a color like gray or black:

enter image description here

To produce the barplots I used:

> table
      Var1 Freq
1  H3K27ac   72
2 H3K27me3   72
3 H3K36me3   72
4  H3K4me1   72
5  H3K4me2   66
6  H3K4me3   72
7   H3K9ac   66
8  H3K9me3   71
> table_filt
      Var1 Freq
1  H3K27ac   31
2 H3K27me3   72
3 H3K36me3    0
4  H3K4me1   71
5  H3K4me2   66
6  H3K4me3   72
7   H3K9ac   60
8  H3K9me3    1

and the code is:

table%>% 
  ggplot(aes(Var1, Freq, fill = Var1))  
  geom_col()   
  scale_fill_manual(values = colours)

table_filt%>% 
  ggplot(aes(Var1, Freq, fill = Var1))  
  geom_col()   
  scale_fill_manual(values = colours)

The colour vector is:

 colours
          H3K27ac          H3K27me3          H3K36me3           H3K4me1           H3K4me2           H3K4me3            H3K9ac           H3K9me3 
"mediumvioletred"         "#E69F00"         "#56B4E9"         "#009E73"         "#F0E442"         "#0072B2"      "firebrick4"      "aquamarine" 

I appreciate any suggestion.

CodePudding user response:

Are you looking for something like this?

library(ggplot2)

ggplot(table, aes(Var1, Freq))  
  geom_col(fill = "gray75")   
  geom_col(data = table_filt, aes(fill = Var1))  
  scale_fill_brewer(palette = "Set1")

  • Related