0

The bars which represent precipitation sums of a season ("Frühling") for each year ("Jahr") ended up lumped into one bar per group (30 years in each of the two reference periods "refp"). Actually, they should be displayed as 30 bars next to each other grouped as "refp1" and after a small space or in another box then 30 bars next to each other grouped as "refp2". I have struggled too many hours now and am still totally clueless.

Even though I followed the instructions of https://www.statology.org/grouped-barplot-in-r/, x=refp worked nicely but the fill=Jahr year bars did not appear next to each other.

This is what the middle of my data frame, at the transition between refp1 and refp2, looks like.

A small code to reproduce the relevant columns of this part would be:

Jahr <- c(1988,1989,1990,1991,1992)
Fruehling <- c(100,75,110,100,150)
refp <- c("refp1","refp1","refp1","refp2","refp2")
refps <- data.frame(Jahr,Fruehling,refp)
  1. With:
ggplot(refps, aes(fill=Jahr, y=Fruehling, x=refp))+geom_bar(position='dodge',stat='identity')

this happened.

  1. With:
ggplot(refps, aes(Jahr,Fruehling,facet_wrap(~refp)))+geom_col()

the two reference periods "refp1" and "refp2" are no longer separated by a space.

  1. Using this approach:
ggplot(data=refps, aes(factor(refp), y=Fruehling, fill=as.factor(Jahr))) + 
     geom_bar(stat="identity", position = "dodge")`

it got closer though I had rather the years beneath the bars instead of individually in the legend and only in one colour per refp group.

I could reduce my expectations and plot everything on its own similar to 2) or use the suggestion shown in 3). But I am really puzzled and would appreciate it a lot if you could explain to me why in 1) with geom_bar the years get lumped in spite of position = 'dodge' and how to get them separate.

Amoebe
  • 3
  • 4
  • 1
    `ggplot(refps, aes(refp, Fruehling, fill = Jahr, group = Jahr)) + geom_col(position = position_dodge())` – Jon Spring May 15 '23 at 23:48
  • Yes! The ´group = Jahr´ is what I'd been missing to separate the bars because the default mapping had failed. Thank you. – Amoebe May 16 '23 at 09:09

1 Answers1

0

What you can do is using facet like this

library(ggplot2)
Jahr <- c(1988,1989,1990,1991,1992)
Fruehling <- c(100,75,110,100,150)
refp <- c("refp1","refp1","refp1","refp2","refp2")
refps <- data.frame(Jahr,Fruehling,refp)

ggplot() + 
  geom_bar(data = refps, mapping = aes(x=Jahr, y=Fruehling), 
           position='dodge',stat='identity') +
  scale_x_continuous(breaks = seq(min(Jahr), max(Jahr), by =1)) +
  facet_grid(cols = vars(refp), scales = "free_x")

Created on 2023-05-17 with reprex v2.0.2

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26
  • That's good, thank you. The only little issue is - I clarified my question now - that there should not be all the empty years in both refp group but just a small separating space. Looked up `?facet_grid` and cutting out empty years was possible with scales="free_x". `ggplot() + geom_bar(data = refps, mapping = aes(x=Jahr, y=Fruehling), position='dodge',stat='identity') + facet_grid(cols = vars(refp),scales="free_x")´ – Amoebe May 16 '23 at 08:53
  • With the free_x there is potention mis_match between bar size across facet... I updated the answer a bit. – Sinh Nguyen May 17 '23 at 01:21