箱线图 y 轴上的自定义刻度数

Custom number of ticks on y axis for a boxplot

示例代码如下:

library("ggpubr")

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

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

它产生情节:

我需要在 y 轴上设置更多刻度。我需要能够设置将显示在 y 轴上的自定义刻度数。例如,在 y5, 10, 15, 20, 25, 30 刻度上显示。如何设置我需要的 y 轴的刻度数?

要添加更多报价,您需要手动为任何 scale_*_continuous() 函数的 breaks 参数指定值。

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))

如果您只想显示刻度但想控制显示哪些标签,您可以通过传递解析函数对 labels 参数进行如下操作。

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 ~ "")
  })

reprex package (v1.0.0)

于 2021-05-11 创建