Keri Keri - 5 months ago 20
R Question

Barplot for volume per year doesn't show all year

I have a dataframe like this:

x <- structure(list(year = structure(c(2008,2009,2010,2011,2012,2013,2014,2015,2016,2017)), value = c(0,11,20,31,1000,10,500,161,20,3)), .Names = c("year", "value"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "data.frame")


I try to make a barplot using this:

library(ggplot2)
ggplot(x, aes(year, value, fill=year)) +
geom_bar(stat="identity", position="dodge")


My problems are:
The x-axis with years have this format 2007.5 (they have the month .5 but I would like to have only the year).
I would like to have in x-axis all years from 2008 and see all years not every 2 years as it is now.

Is there any possible way to fix it?

Answer Source

Since year is numeric, it creates numeric axis.

You can fix this by replacing year with as.character(year)

ggplot(x, aes(as.character(year), value, fill=year)) + 
geom_bar(stat="identity", position="dodge")