I'm trying to plot the number of exoplanets discovered per year, grouped by the method of discovery. My data is of this kind: planet|discovery method|year
I have this command:
ggplot(data, aes(x=pl_disc, fill=pl_discmethod)) +
geom_bar() +
scale_fill_brewer(palette="RdBu") +
labs(x="Years", y="Count",
title="Number of exoplanets discovered per year",
fill="Discovery method")+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
scale_x_continuous("Years", labels=data$pl_disc, breaks=data$pl_disc)
And getting this plot:
I need a little different version of this plot.
- The years on the x-axis are incomplete. When there aren't values to plot for a given year, the year is not shown. I want all the years to be shown
- I also need to show the sum of the elements in each column. I tried this:
ggplot(data, aes(x=pl_disc, fill=pl_discmethod)) +
geom_histogram(binwidth = 1) +
scale_fill_brewer(palette="RdBu") +
stat_bin(aes(label=..count..),vjust=-1, geom="text", binwidth = 1) +
labs(x="Years", y="Count",
title="Number of exoplanets discovered per year",
fill="Discovery method")
But the values are overwritten, as seen in this plot:
How can I have the complete scale of years and the count of each column?