0

I have a stacked & grouped plot. I have numbers of tomatoes and apples (two groups) for 3 different years (stacked). I want that the years of the tomotoes have a different color than the years of the apples, so that is it clear which stack is about which fruit. So, for example, first year of tomatoes is dark-blue and for apples is light-blue, second year of tomatoes is dark-green and for apples light-green.

My currunt code is this:

 ggplot() +
      geom_bar(data = dataset,
               mapping = aes(x = var_2, y = number , fill = as.factor(year)),
               stat="identity",
               position='stack',
               width = 35)  +
      geom_bar(data = dataset,
               mapping = aes(x = as.numeric(var_1) + 35 + 0.05 , y = number, fill = as.factor(year)),
               stat="identity",
               position='stack',
               width = barwidth)+
      scale_x_discrete(labels = function(x) str_wrap(x, width = 40)) +
      #scale_fill_manual(values = #current colors but that doesnt work +
      coord_flip()

Some ideas here maybe?

Edit: also fine to add a pattern to one of the two types of fruit.

my current figure

LisanneB
  • 3
  • 2
  • 3
    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. If you want to post your data type `dput(NAME_OF_DATASET)` into the console and copy the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 20))` for the first twenty rows of data. – stefan Oct 03 '22 at 10:03

0 Answers0