问题描述
这是示例代码:
library("ggpubr")
# Load data
data("ToothGrowth")
df <- ToothGrowth
# Basic plot
ggBoxplot(df,x = "dose",y = "len",width = 0.8)
它产生情节:
我需要在 y
轴上有更多刻度。我需要能够设置将在 y
轴上显示的自定义刻度数。例如在 y
轴 5,10,15,20,25,30
刻度上显示。如何为我需要的 y
轴设置刻度数?
解决方法
要添加更多刻度,您需要手动为任何 breaks
函数的 scale_*_continuous()
参数指定值。
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,5),labels = function(x){
case_when(x%%10==0 ~ as.character(x),TRUE ~ "")
})
由 reprex package (v1.0.0) 于 2021 年 5 月 11 日创建