gpt4 book ai didi

r - 在没有标签的情况下向 ggplot 添加小的中断

转载 作者:行者123 更新时间:2023-12-05 06:57:15 26 4
gpt4 key购买 nike

我想在使用 ggplot() 生成的图形上添加minor 轴刻度和/或线。这是一项看似简单的任务,但事实证明非常具有挑战性。我找到了 this solution但它相对不优雅,不适合我的应用程序。我将使用不同的数据多次重新制作我的绘图,从而更改轴限制并通过 geom_vline() 打破任何手动规范。

我阅读了 scales::minor_breaks_width() 的文档,并尝试将它用于 scale_x_continuous()minor_breaks 参数,但它抛出关于“循环中断”的错误。

下面是一个reprex,谁能给个建议?

library(tidyverse)

p <- mpg %>%
ggplot(aes(displ, hwy))+
geom_point()

# major breaks are easy to set
p +
scale_x_continuous(
breaks = scales::breaks_width(width = 0.5, offset = 0))

# adding a minor breaks specification throws an error 
p +
scale_x_continuous(
breaks = scales::breaks_width(width = 0.5, offset = 0),
minor_breaks = scales::minor_breaks_width(width = 0.01, offset = 0) )
#> Error in loop_breaks(range, breaks, f): argument "breaks" is missing, with no default

reprex package 创建于 2020-11-19 (v0.3.0)

最佳答案

或者,您可能想尝试一种更简单的方法。我使用 0.1 的宽度,因为 0.01 很难看到。

library(tidyverse)
p <- mpg %>%
ggplot(aes(displ, hwy))+
geom_point()

brks = seq(1.5, 7, 0.1)
lbs <- ifelse(brks%%0.5 == 0, format(brks, digits = 1, nsmall = 1), "")
p +
scale_x_continuous(
breaks = brks, labels = lbs)

enter image description here

关于r - 在没有标签的情况下向 ggplot 添加小的中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64922259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com