Is it possible to get a shaded fill in R or to overlap 2 identical barplots in order to represent 2 variables?

19 Views Asked by At

I'm a beginner. I have this barplot which sums up all the sales per month. Colors are showing the cities. My concern is to shade somehow these colors in order to show the year too. I made 2 barplots and since I don't know how to put eveything in one single barplot, I was ondering if there's a way to merge these graphs? Even better, if there's a way to associate 2 variables to "fill" it would be much easier. I know theres already col="black" which kind of shows the years, but it's not enough! I want different shades of the same color per city!

Barplot with fill=city Barplot with fill=year

Here's the code with fill=year:

ggplot(data=dati)+
    geom_col(aes(x=month, y=sales, fill=year),
           position = "stack", 
           col = "black")+
    labs(title = "Distribuzione delle vendite",
         x = "Month",
         y = "Sales")+
    theme_classic()

I tried this alternative (fill=city):

dati %>%
    group_by(city, year, month)%>%
    summarise(vendite=sum(sales), .groups = 'drop')%>%
    ggplot(aes(x=month, y=vendite, fill=city))+
    geom_col(col="black")

which is the same.

I tried fill=cbind(city,year) and to put fill in different places with those 2 different variables but it didn't work.

Thanks to anyone who's going to reply!

1

There are 1 best solutions below

0
Maria Bragarenco On

I made it! I added alpha=year to my code and I got this:

Final graph