强制单独的 y 轴限制

Force individual y axis limits

我有这个数据框:

df <- structure(list(x = c(2L, 3L, 4L, 5L, 1L), y = c(24L, 27L, 18L, 
23L, 28L)), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5"))

  x  y
1 2 24
2 3 27
3 4 18
4 5 23
5 1 28

我想将 y 轴限制设置为 039 但我得到 040

这是我的代码:

library(ggplot2)

ggplot(df, aes(x, y))+
  geom_point()+
  ylim(0,39)

您可以将 scale_y_continuous 函数中的 limitsbreaks 更改为任何您想要的。要可视化,您可以使用此代码:

df <- structure(list(x = c(2L, 3L, 4L, 5L, 1L), y = c(24L, 27L, 18L, 
                                                      23L, 28L)), class = "data.frame", row.names = c("1", "2", "3", 
                                                                                                      "4", "5"))

    library(ggplot2)
    
    ggplot(df, aes(x, y))+
      geom_point()+
      scale_y_continuous(limits = c(0, 39), breaks = seq(0, 39, by = 13))

输出:

您可以根据需要使用 breaks 参数设置中断。休息不需要是有规律的。

  df <- structure(list(x = c(2L, 3L, 4L, 5L, 1L), y = c(24L, 27L, 18L, 
                                                        23L, 28L)), class = "data.frame", row.names = c("1", "2", "3", 
                                                                                                        "4", "5"))
  
  
  library(ggplot2)
  
  ggplot(df, aes(x, y))+
    geom_point()+
    scale_y_continuous(limits = c(0, 39),
                       breaks = c(0, 10, 20, 30, 39))

reprex package (v2.0.1)

于 2022 年 3 月 20 日创建