Background
I have a dataframe, df
, of athlete injuries:
df <- data.frame(number_of_injuries = c(1,2,3,4,5,6),
number_of_people = c(73,52,43,12,7,2),
stringsAsFactors=FALSE)
The Problem
I'd like to use ggplot2
to make a bar chart or histogram of this simple data using geom_bar
or geom_histogram
. Important point: I'm pretty novice with ggplot2
.
I'd like something where the x-axis shows bins of the number of injuries (number_of_injuries
), and the y-axis shows the counts in number_of_people
. Like this (from Excel):
What I've tried
I know this is the most trivial dang ggplot
issue, but I keep getting errors or weird results, like so:
ggplot(df, aes(number_of_injuries))
geom_bar(stat = "count")
Which yields:
I've been in the tidyverse reference website for an hour at this and I can't crack the code.
CodePudding user response:
It can cause confusion from time to time. If you already have "count" statistics, then do not count data using geom_bar(stats = "count")
again, otherwise you simply get 1 in all categories. You want to plot those values as they are with geom_col
:
ggplot(df, aes(x = number_of_injuries, y = number_of_people)) geom_col()