Background
This could be a duplicate question, but I've gone through several posts and haven't found exactly what I'm looking for. I have a nice ggplot
I made using geom_col
:
It comes from a df
that looks like this:
And I made it using this code:
ggplot(data = avg_repeats_per_year, aes(x = year, y = ratio))
geom_col()
You can see that each number in ratio
corresponds to a different year
from 2009 to 2018 (with one NA
year).
The Problem
You can see that each bar corresponds to a year in year
, but only 3 years are actually labeled on the x-axis of the plot.
How can I get each bar to have a label for its respective year?
What I've Tried
I've made attempts of this sort:
ggplot(data = avg_repeats_per_year, aes(x = year, y = ratio))
geom_col()
scale_x_discrete(breaks = avg_repeats_per_year$year, labels = avg_repeats_per_year$year)
But nothing shows up. Any thoughts?
CodePudding user response:
It seems to be numeric column, change it to factor
or character
and it should work
ggplot(data = avg_repeats_per_year, aes(x = factor(year), y = ratio))
geom_col()
scale_x_discrete(labels = avg_repeats_per_year$year)
xlab("year")
-output
data
avg_repeats_per_year <- structure(list(year = c(2009L, 2010L, 2011L, 2012L, 2013L, 2014L,
2015L, 2016L, 2017L, 2018L, NA), ratio = c(2.28, 2.24, 2.3, 2.45,
2.32, 2.41, 2.56, 2.52, 2.63, 1.32, 0.56)), class = "data.frame", row.names = c(NA,
-11L))