ggplot - Display every values on an axis


We want to display a chart with ggplot, the xaxis is constituted with years and we want every year to appear on it.

data <- data.frame("year"=c(2000, 2005, 2006, 2007), "value"=c(10, 25, 20, 30))
ggplot(data) + geom_col(aes(x=year, y=value))

data is:

> data
  year value
1 2000    10
2 2005    25
3 2006    20
4 2007    30

In the x-axis, some years are not displayed


Give values to the axis with scale_x_continuous

xlabels <- min(data$year):max(data$year)
ggplot(data) + geom_col(aes(x=year, y=value)) +
  scale_x_continuous(labels = xlabels, breaks=xlabels)


We could transform year in a factor but this would remove the missing years. However it can be on purpose.

ggplot(data) + geom_col(aes(x=as.factor(year), y=value)) 

How can we help?

Whether you already know about your data pain points or just want to have a friendly conversation, send us a message and tell us who you are.