1

I would like to plot exactly this:

df <- data.frame("col1" = c('a', 'b', 'a'), "col2" = c(1, 2, 3))
barplot(df$col2, names.arg = df$col1)

but with ggplot2 library. geom_bar() or geom_col() everytime groups col1 categories for me.

Thank you very much for all answers.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
pacaklu
  • 13
  • 3
  • Why do you want to have two columns with the same name? This is confusing... are they from different years or different data? If they are unrelated they shouldn't be named the same... – kath Nov 02 '18 at 16:17

1 Answers1

0

ggplot doesn't like to have two columns with the same name because that makes the plot very hard to interpret it. You can force this behavior by creating unique groups and manually setting the axis labels to it looks like they are the same name. For example

df2 <- df %>% mutate(col3 = col1 %>% as.character %>% make.unique %>% factor(., levels=.))
ggplot(df2, aes(col3, col2)) + 
  geom_col() + 
  scale_x_discrete(breaks=df2$col3, labels=df2$col1)

resulting plot

MrFlick
  • 195,160
  • 17
  • 277
  • 295