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...
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...
In base R, you can first aggregate
and then use barplot
.
barplot(Snowfall~Location, aggregate(Snowfall~Location, df, sum))
Here a tidyverse
approach
df <-
tibble::tribble(
~Location, ~Snowfall,
"London", 0.4,
"London", 1.3,
"NYC", 4.3,
"NYC", 0.2,
"Paris", 3,
"London", 2
)
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)