Trying to create a data frame for grouped bar plot

Hello, I am trying to create a data frame in order to make a grouped bar plot. Here is what I have currently

#create data frame
df <- data.frame(team=rep(c('CpG 1', 'CpG 2', 'CpG 3', 'CpG 4', 'CpG 5', 'CpG 6, 'CpG 7')), each=7),
position=rep(c('l/l', 'l/s', 's/s'), times=7),
points=c(9.3, 12.5, 16.5, 13.2, 17.1, 22.8, 6.4, 4.8, 9.4, 6.3, 9.2, 12.1, 8.5, 12, 17.8, 9.6, 12.7, 18.7, 9.9, 8.8, 17.2)

#view data frame
df

I cannot find similar answers and I am very new to R, any help would be appreciated.

Hello,

The following code produces grouped bar plots:

library(ggplot2)

df <- data.frame(team=rep(c('CpG 1', 'CpG 2', 'CpG 3', 'CpG 4', 'CpG 5', 'CpG 6', 'CpG 7'), each=3),
                 position=rep(c('l/l', 'l/s', 's/s'), times=7),
                 points=c(9.3, 12.5, 16.5, 13.2, 17.1, 22.8, 6.4, 4.8, 9.4, 6.3, 9.2, 12.1, 8.5, 12, 17.8, 9.6, 12.7, 18.7, 9.9, 8.8, 17.2))

# Stacked
ggplot(df, aes(x = team, y = points, fill = position)) +
  geom_col()

# Dodged
ggplot(df, aes(x = team, y = points, fill = position)) +
  geom_col(position = position_dodge2())

1 Like

Thank you very much!

This topic was automatically closed 7 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.