【问题标题】:Why is the ylim() function in ggplot2 being ignored?为什么 ggplot2 中的 ylim() 函数被忽略?
【发布时间】:2020-01-14 16:43:46
【问题描述】:

我希望折线图中的 y 轴显示更广泛的值,因为上限和下限数据点远离轴上显示的默认最大值和最小值 - 我想将限制设置为 -0.07和 0.07。但是,设置 y 轴的代码似乎被忽略了——这是我的代码:

scale<-c("250 m", "500 m", "1 km", "2 km", "3 km", "4 km", "5 km")
scales<-factor(scale, levels=c("250 m", "500 m", "1 km", "2 km", "3 km", "4 km", "5 km"))

coefs<-c(-0.069, -0.023, -0.006, 0.041, 0.069, 0.066, 0.07)
coef.scales=data.frame(scales,coefs)

coef.fig<-ggplot(data = coef.scales, aes(scales, coefs, group = 1))+
  geom_point() +
  geom_line() +
  labs(x = "Scale", y = "Standardized coefficient") + 
  theme_classic(base_size = 17) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  ylim(-0.07, 0.07)

除了 ylim(-0.07, 0.07) 命令,我还尝试了 scale_y_continuous(-0.07, 0.07) 和 coord_cartesian(ylim = c(-0.07, 0.07))。默认数字保持不变,在 y 标度上有 3 个值,范围从 -0.04、0.04。 为什么我不能更改 y 轴上显示的值?

【问题讨论】:

    标签: r ggplot2 axis yaxis


    【解决方案1】:

    我认为您正在尝试设置breaks。您的代码实际上确实设置了 y 限制。它只是没有按照您的预期标记它们。

    coef.fig<-ggplot(data = coef.scales, aes(scales, coefs, group = 1))+
      geom_point() +
      geom_line() +
      labs(x = "Scale", y = "Standardized coefficient") + 
      theme_classic(base_size = 17) +
      geom_hline(yintercept = 0, linetype = "dashed") +
      scale_y_continuous(limits = c(-0.07, 0.07), breaks = c(-0.07, 0, 0.07))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多