plot the frequency with multiple groups

I would like to create a bargraph with four clusters of 3 bars, with the frequency of each season for bananas, apples and pears, with each group in the same color and indicate this in a legend. The frequency of each season must total 100% .
My dataframe is like:

group        season
bananas      season 1
bananas      season 1
bananas      season 2
bananas      season 3
apples       season 1
apples       season 2
apples       season 3
pears        season 1
pears        season 2
pears        season 3
pears        season 3
pears        season 1

I used count to get this dataframe:

library(tidyverse) 
df<- df %>% 
  count(season,group)

#  group        season         value
# bananas       season 1        2
# bananas       season 2        1
# bananas       season 3        1
# apple         season 1        1
# apple         season 2        1
# apple         season 3        1
# pears         season 1        2
# pears         season 2        1
# pears         season 3        2

Something like this?

library(tidyverse)

df <- tibble(group = sample(c("bananas", "apples", "pears"), 15, replace = TRUE),
             season = sample(c("season 1", "season 2", "season 3"), 15, replace = TRUE))

df2 <- df %>% 
  group_by(group) %>% 
  count(season) %>% 
  mutate(prop = n / sum(n))

ggplot(df2, aes(group, prop, fill = season)) +
  geom_col()

![image|482x401](upload://vhT4lCJtJjEKWWlKqfZMzofgcut.png)

Next time make your dataset reproducible:

Thanks, I will keep in mind the format for future posts. No really, I want like the following photo, but the frequency on y-axis . The frequency of each season must total 100%, like this: Season1: banana pears+apple=100%, the same for other seasons
b497d70a-9287-4a8f-b562-3d2f5c16355b

Play around with the numbers and use position_dodge in ggplot2. See how you go.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.