Search code examples
rggplot2axis-labels

Modifying ggplot2 Y axis to use integers without enforcing an upper limit


I am trying to modify the axes in ggplot2 so that it is one decimal point and has a label for every integer. However, I want to do it without an upper limit so that it will automatically adjust to data of different counts.

The difference between my question and the question posed here (that I was flagged as being a duplicate of) is that I need to make this work automatically for many different data sets, not just for a single one. It must automatically choose the upper limit instead of creating a fixed y-axis with breaks=(0,2,4,...). This question has been answered extremely well by @DidzisElferts below.

Here is my work:

library(data.table)
library(scales)
library(ggplot2)

mtcars <- data.table(mtcars)
mtcars$Cylinders <- as.factor(mtcars$cyl)
mtcars$Gears <- as.factor(mtcars$gear)
setkey(mtcars, Cylinders, Gears)
mtcars <- mtcars[CJ(unique(Cylinders), unique(Gears)), .N, allow.cartesian = TRUE]

ggplot(mtcars, aes(x=Cylinders, y = N, fill = Gears)) + 
               geom_bar(position="dodge", stat="identity") + 
               ylab("Count") + theme(legend.position="top") + 
               scale_x_discrete(drop = FALSE)

Cylinder Graph

As you can see, ggplot2 is plotting the axes with a decimal point and doing it every 2.5 automatically. I'd like to change that. Any way to do so?


Solution

  • Use scale_y_continuous(breaks=c(0,2,4,6,8,10)). So your plotting code will look like:

    ggplot(mtcars, aes(x=Cylinders, y = N, fill = Gears)) + 
      geom_bar(position="dodge", stat="identity") + 
      ylab("Count") + theme(legend.position="top") + 
      scale_y_continuous(breaks=c(0,2,4,6,8,10)) +
      scale_x_discrete(drop = FALSE)
    

    EDIT: Alternatively you can use scale_y_continuous(breaks=seq(round(max(mtcars$N),0))) in order to automatically adjust the scale to the maximum value of the y-variable. When you want the breaks more then 1 from each other, you can use for example seq(from=0,to=round(max(mtcars$N),0),by=2)