vasili111
vasili111

Reputation: 6930

Custom number of ticks on y axis for a boxplot

Here is sample code:

library("ggpubr")

# Load data
data("ToothGrowth")
df <- ToothGrowth

# Basic plot
ggboxplot(df, x = "dose", y = "len", width = 0.8)

It produces plot:

enter image description here

I need to have more ticks on y axis. I need to be able to set a custom number of the ticks that will be displayed on y axis. For example to make display on y axis 5, 10, 15, 20, 25, 30 ticks. How can set the number of ticks for y axis that I need?

Upvotes: 0

Views: 1352

Answers (1)

Justin Landis
Justin Landis

Reputation: 2071

To add more ticks you need to specify the values manually to the breaks argument of any scale_*_continuous() function.

library(ggpubr)
#> Loading required package: ggplot2
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
# Load data
data("ToothGrowth")
df <- ToothGrowth

# Basic plot
ggboxplot(df, x = "dose", y = "len", width = 0.8) +
  scale_y_continuous(breaks = seq(5, 30, 5))

If you only wanted the ticks to be present but wanted to control which labels were displayed, you could do something like the following with the labels argument by passing a parsing function.

ggboxplot(df, x = "dose", y = "len", width = 0.8) +
  scale_y_continuous(breaks = seq(5, 30, 5), labels = function(x){
    case_when(x%%10==0 ~ as.character(x),
              TRUE ~ "")
  })

Created on 2021-05-11 by the reprex package (v1.0.0)

Upvotes: 2

Related Questions