Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

barplot ggplot with bars filled by values of another df

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading