0

Hello stack overflow community,

I'm trying to find a way to colour two columns that I managed to put together in a stacked barplot. The X axis represents the months, and the columns represent both the year (there is 'n-1' year and 'n' year) and type of business. Type of business was easy to colour because it is the fill, but I can't find a way to have different colours for 'n-1' year and 'n' year columns, or a way to clarify that each column per month represents a different year.

For clarity purposes, this is what the data looks like: enter image description here

This is the code I used so far:

barwidth = 0.35
ggplot() + 
  geom_bar(data = BUSINESS_18,
           mapping = aes(x=Month, y=GWP_mio, fill=Business),
           stat ="identity",
           position = "stack",
           width = barwidth) +        
  geom_bar(data = BUSINESS_19,
         mapping = aes(x=as.numeric(Month) + as.numeric(barwidth) + 0.05, y=GWP_mio, fill=Business),
         stat ="identity",
         position = "stack",
         width = barwidth) +
  theme_ipsum() +
  scale_fill_manual(values = c("#009E73","Darkblue")) +
  scale_y_continuous(breaks=seq(0,18000000,1000000), labels = scales::comma_format(scale = 1/1000000, 
  accuracy = .1), limits = c(0,18000000)) +
  ggtitle('GWP development') +
  theme(plot.title = element_text(hjust=0.5, size=14, family="Calibri", face="bold"),
        legend.title = element_text(size=11, family="Calibri", face="bold"),
        axis.title.x = element_text(hjust=1, size=11, family="Calibri", face="bold"),
        axis.text.x = element_text(angle=0, hjust=1, size=11, family="Calibri"),
        axis.title.y = element_text(angle=0, hjust=1, size=10, family="Calibri", face="bold"))

And a picture of the graph, so you can more easily understand what I mean (no differentiation between the two columns per month): enter image description here Any help would be highly appreciated! Many thanks in advance.

1 Answers1

0

Using a facet layer here would be a better approach, but at this point I am pretty sure you already know how to do that.

My recomendation is to create a new variable, pasting together yera and business and using this one as the fill aesthetic.

data

df <- data.frame(
  year = rep(c(2017, 2018), each = 24),
  gwp = rnorm(48, mean = 10000, sd = 300),
  month = rep(1:12, 4),
  business = rep(
    c(
      rep("new", 12),
      rep("renewal", 12)
    )
  )
)

df <- df %>%
  mutate(
    fill = paste(business, year, sep = "-")
  )

plot

  ggplot() +
  geom_col(
    data = df %>% filter(year == 2018),
    aes(x = month, fill = fill, y = gwp),
    width = barwidth
  ) +
    geom_col(
      data = df %>% filter(year == 2017),
      aes(x = month + as.numeric(barwidth) + 0.05, fill = fill, y = gwp),
      width = barwidth
    )

enter image description here

If you create a factor with the new variable and order the legend labels as your wish.

Another way to do this would be controlling the apha, you can see how to do it in the answer to this question

Johan Rosa
  • 2,797
  • 10
  • 18