Grouped barplot with non-numeric x axis (ggplot)

Hello!

I'm looking at the change in traffic pre-lockdown vs during lockdown on UK roads, I want to make a graph that looks like this (made in excel)

I can't quite recreate this in R, and I think it might be because R isn't reading 'road' as a valid x axis...

This is the code I've used so far, but sadly it isn't working and I'm stuck on what to do from here:

road =c('Minor Roads' ,'A Roads', 'Motorways')

df_roads<- data.frame(Road_types= road,
PLD = c(127,145,68),
LD = c(112, 115, 53))

df_roads_perc <- df_roads %>%
pivot_longer(2:3, names_to = "category", values_to = "observations" ) %>%
mutate(category = factor(category, levels = c('PLD', 'LD')))

df_roads_perc %>% ggplot(aes(x=road, y=observations, fill = category))+
geom_col(position = "dodge" )+
theme_minimal()+
labs(x= 'Type of Road', y='Vehicle Miles (bn)',
title= '') +
theme(plot.title = element_text(hjust = 0.5, size= 10, face= 'bold'),
legend.position = "bottom") +
scale_fill_manual('category', values= c('#a64ca6', '#4ca64c', 'blue3'))

Any help would be greatly appreciated!
Thanks!
J

The data frame df_roads_perc has a column named Road_types but not one named road. Try setting

df_roads_perc %>% ggplot(aes(x=Road_types, ...
1 Like

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.