【问题标题】:Custom number of ticks on y axis for a boxplot箱线图 y 轴上的自定义刻度数
【发布时间】:2021-05-11 15:33:12
【问题描述】:

这里是示例代码:

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 轴的刻度数?

【问题讨论】:

标签: r ggplot2 ggpubr


【解决方案1】:

要添加更多刻度,您需要手动为任何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 创建

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-21
  • 2019-02-08
相关资源
最近更新 更多