Expand Categorical x-axis in ggplot

前端 未结 2 836
遥遥无期
遥遥无期 2021-01-19 18:48

I can\'t figure out how to use expand() within scale_x_discrete() to expand a categorical x-axis so that placing a label to the right of points won

2条回答
  •  执念已碎
    2021-01-19 19:12

    Since I'm facing the same problem... I found expand_limits(...) quite useful here:

    dat <- data.frame(x= c('a', 'b', 'c'), y= 1:3)
    ggplot(data= dat, aes(x= x, y= y)) + 
        geom_point() + 
        expand_limits(x= c(-1, 5))
    

    Or, to avoid hardcoding the limits:

        expand_limits(x= c(-1, length(levels(dat$x)) + 2))
    

    (With ggplot2_3.1.0)

提交回复
热议问题