0

I have a plot of 27 columns. I want to color them in a scale of greens for example. But I want them to be blocked in groups of three. E.g. first three columsn light green, then next three columns a darker shade of green and so on and so on. Currently the colors are just repeating themselves.

current code

    # plot average for each ellenberg category for each quadrat
i = 1
for (df in ellenCatTab){
  plotName <- names(ellenCatTab)[i]
  i = i + 1
  col_means <- colMeans(df[,5:31], na.rm=TRUE)
  col_meansdf <- stack(col_means)
  p <- ggplot(col_meansdf, aes(x=ind, y=values, fill=ind)) + 
           ggtitle(plotName) +
           geom_col() +
           theme_classic() +
           scale_fill_manual(values=rep(c("darkseagreen1", "darkolivegreen1", "darkseagreen4"), 9), drop=FALSE) +
           theme(legend.position='none')
        
  plot(p)
}

current output

I want for example all the XI column to be the same color. Then the X2 to be different.

enter image description here

Melanie Baker
  • 449
  • 1
  • 13
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Aug 17 '22 at 07:56
  • 1
    This said: Add an identifier for your groups of three to your data, e.g. from your image something like `col_meansdf$group <- gsub("^(X\\d+).*$", "\\1", col_meansdf$ind)`should work. Then map this id variable on the fill aes. – stefan Aug 17 '22 at 07:57

1 Answers1

0

Adding this work, as suggested in the comments.

col_meansdf$group <- gsub("^(X\\d+).*$", "\\1", col_meansdf$ind)
Quinten
  • 35,235
  • 5
  • 20
  • 53
Melanie Baker
  • 449
  • 1
  • 13