Grouped barplot must be grouped

A collection of common dataviz caveats by

The issue

A barplot displays the value of a numeric variable for several entities. These entities can be grouped using a categoric variable, resulting in a grouped barplot.

When building a grouped barplot, make sure that your bars are indeed grouped: bars within a group must be closer to one another than bars in different groups. In the example below, bars are not grouped, making hard to distinguish groups.

# Libraries

# Load dataset from github
data <- babynames %>% 
  filter(name %in% c("Anna", "Mary")) %>%

# A grouped barplot
data  %>% 
  filter(year %in% c(1950, 1960, 1970, 1980, 1990, 2000)) %>%
  mutate(year=as.factor(year)) %>%
  mutate( nameYear = paste(year, name, sep=" - ")) %>%
  ggplot( aes(x=as.factor(nameYear), y=n, fill=name)) +
    geom_bar(stat="identity") +
    scale_fill_viridis(discrete=TRUE, name="") +
    theme_ipsum() +
    ylab("Number of baby") +
    xlab("") +
      axis.text.x = element_text(angle=60, hjust=1)