Keep Unused Factor Levels in ggplot2 Barplot in R
In this article, we will discuss how to keep unused factor levels in ggplot2 barplot in the R programming language.
In this approach to keep unused factor levels in ggplot2 barplot, the user first needs to install and import the ggplot2 package in the R console and plot the barplot of the data consisting of zero values with the geom_bar() and ggplot() function called with the required parameters and then call the scale_x_discrete() function with the drop argument to be set as false form the ggplot2 package to keep unused factor levels in the R programming language.
scale_x_discrete(…, expand = waiver())
- …: common discrete scale parameters: name, breaks, labels, na.value, limits and guide.
- expand: a numeric vector of length two giving multiplicative and additive expansion constants. These constants ensure that the data is placed some distance away from the axes.