1

For example, let the data frame, df, be

Location Snowfall
London 0.4
London 1.3
NYC 4.3
NYC 0.2
Paris 3.0
London 2.0

Is there a way to make a bar graph of the total snowfall by location?

London's bar length would be 3.7 and so on...

2 Answers2

2

In base R, you can first aggregate and then use barplot.

barplot(Snowfall~Location, aggregate(Snowfall~Location, df, sum))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

Here a tidyverse approach

Data

df <-
tibble::tribble(
  ~Location, ~Snowfall,
   "London",       0.4,
   "London",       1.3,
      "NYC",       4.3,
      "NYC",       0.2,
    "Paris",         3,
   "London",         2
  ) 

Plot

library(dplyr)
library(ggplot2)


df %>% 
  #Grouping by Location
  group_by(Location) %>% 
  #Sum of Snowfall by Location
  summarise(Snowfall = sum(Snowfall)) %>% 
  ggplot(aes(Location,Snowfall))+     
  geom_col(width = .5)

enter image description here

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32