Home > Software design >  My bar chart only prints a single bar from my data
My bar chart only prints a single bar from my data

Time:03-01

When using the following data and code my bar char only prints 1 bar, any idea on how to fix this?

life_bar
# A tibble: 6 × 2
  Continent                mn
  <chr>                 <dbl>
1 Africa                 41.1
2 Americas               19.5
3 Eastern Mediterranean  47.0
4 Europe                 15.6
5 South-East Asia        37.7
6 Western Pacific        16.4

Code used;

ggplot(life_bar, aes(x = 'mn'))  
geom_bar(fill = 'green', col = 'black')

CodePudding user response:

You need to specify both x and y variables. x should be Continent and y should be mn. You don't quote the column names.

Also when using geom_bar() you need to specify how the values should be aggregated - in your case geom_bar(stat = "identity"). You can avoid that by using geom_col().

ggplot(life_bar, aes(x = Continent, y = mn))   
geom_col()

There's lots of built-in help in R e.g. ?geom_bar and extensive online help too.

  • Related