【问题标题】:When plotting using geom_abline with ggplot, there are no tick marks on the axes even when specified使用带有 ggplot 的 geom_abline 进行绘图时,即使指定了坐标轴上也没有刻度线
【发布时间】:2019-10-03 15:32:27
【问题描述】:

我正在使用 geom_abline 绘制线条,当我只绘制线条时,我轴上的所有刻度线都会消失。我不知道为什么或如何把它们放回去

library( tidyverse )

dat = data.frame( id = 1:3,
                  pi.0 = c( 0, 1, 2 ),
                  pi.1 = c(0.15, 0.05, -0.35 ) )

ggplot( dat, aes( group=id ) ) +
  geom_abline( aes( intercept=pi.0, slope=pi.1 ) ) +
  coord_cartesian( xlim=c(0,9), ylim=c( -5, 7 ) ) +
  scale_x_continuous( breaks=0:9 ) +
  labs( y="Outcome", x="Time" )

我希望在 x 轴上的 0、1、...、9 处看到刻度线。但我没有!为什么不?我该如何取回它们?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您应该使用scale_*_continuous 来设置限制,而不是coord_cartesian

    library(ggplot2)
    dat = data.frame( id = 1:3,
                      pi.0 = c( 0, 1, 2 ),
                      pi.1 = c(0.15, 0.05, -0.35 ) )
    
    ggplot(dat, aes( group=id ) ) +
      geom_abline( aes(intercept=pi.0, slope=pi.1 ) ) +
      scale_x_continuous(breaks=c(0:9), limits=c(0, 9) ) +
      scale_y_continuous(limits=c( -5, 7 ) ) +
      labs( y="Outcome", x="Time" )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      相关资源
      最近更新 更多