Use of scale_x_discrete in R ggplot2

That probably already happens when you add g + scale_x_discrete(). This happens when using a discrete scale for continuous data. Without the breaks, you can see the wrong limits and just change them.

g + scale_x_discrete()
g + scale_x_discrete(limits=1:7)
g + scale_x_discrete(limits=1:7, labels = letters[1:7])

Alternatively, you can use factor to get the proper limits from the beginning. Of course you’ll have to rename the axis.

ggplot(data=plottingData, aes(x=factor(x), y=y, ymin=ymin, ymax=ymax)) +
  geom_bar(stat="identity", fill=col) +
  geom_errorbar(width=0.5*binwidth, size=0.3) +
  scale_x_discrete(name = 'x')

Leave a Comment