Search code examples
rggplot2geom-bar

ggplot showing consolidated stats instead summary breakdown of a variable


ggplot is not showing the full summary/grouping stats of a variable, but instead a consolidated number (ie total count) on the variable.

I tried googling and researching to no avail.

mydatawlocation %>% 
  group_by(`General Location`) %>% 
  summarise(count_level = n()) %>% 
  ggplot(aes(x = as.factor('General Location'), y = count_level)) +
  geom_bar(stat = 'identity')

The General Location variable has multiple locations eg "East", "West". I want to get a breakdown of total count for each (East/West/etc). Instead my code is churning out "General Location" on x-axis and total count on y-axis, without the breakdown


Solution

  • Try the following code:

    mydatawlocation %>% 
    group_by(`General Location`) %>% 
    summarise(count_level = n()) %>% 
    ggplot(aes(x = as.factor(`General Location`), y = count_level)) +
    geom_bar(stat = 'identity')
    

    I have replaced

    as.factor('General Location')  # using '' 
    

    with

    as.factor(`General Location`) # using ``