0

I was planning to produce a graph similar to this, which uses frequency data, but I can't seem to figure it out.

Got it from an journal article

I was thinking that the legends below were just another barplot that the user have integrated with the original barplot. But I cannot produce the same barplot which utilizes a similar color for each bar it represents.

enter image description here

library(ggpubr)
library(colorspace)
library(ggsci)
ggbarplot(codons, width = 0.8,
          y = "Count", Ticks = FALSE, sort.by.groups = TRUE,
          x = "Amino_acid", Color = "Count", fill = "Count",
          font.tickslab = "white",
          legend = "none", 
          xlab = NULL,ylab = NULL, 
          position = position_fill(), ggtheme = theme_void())

I can get this. Though I can put labels inside, I cannot use other color palette aside from a blue gradient.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
dex
  • 1
  • You could use {ggplot2} as @rodrigocfaria suggested and color the axis tick lables as explained here: https://stackoverflow.com/questions/38862303/customize-ggplot2-axis-labels-with-different-colors – I_O Dec 21 '22 at 18:50

1 Answers1

0

You can try it with pure ggplot2 combined with viridis. Something like this:

library(ggplot2)
library(viridis)

Category1 <- c("a","b","c","c","a","d","c","c","c","d")
Category2 <- c("x","x","z","w","x","y","z","w","x","x")
Count <- c(11,12,12,25,10,8,9,10,7,6)

codons <- data.frame(Count, Category1, Category2)

ggplot(codons, aes(fill=Category1, y=Count, x=Category2)) + 
    geom_bar(position="fill", stat="identity") +
    scale_fill_viridis(discrete = T)

enter image description here

Then you can set different color scales following this reference: https://sjmgarnier.github.io/viridis/reference/scale_viridis.html#arguments.

rodrigocfaria
  • 440
  • 4
  • 11
  • This one is helpful, but I have around 63 values as suggested by the previous graph that I made. When I used Viridis it is only putting up to 4 colors only. What if I got more than 6 values per bar? – dex Dec 23 '22 at 07:38
  • @dex I updated the example to be more specific for your case. The main change is that I included two categories instead of only one. I believe that matches your data pattern. – rodrigocfaria Dec 23 '22 at 15:12