ggplot2 bar plot, no space between bottom of geom and x axis keep space above

前端 未结 6 1093
逝去的感伤
逝去的感伤 2020-11-30 00:37

When I plot a bar graph in ggplot2 I would like to reduce the space between the bottom of the bars and the x-axis to 0, yet keep the space above the bars and the plot box.

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 01:04

    You can expand the limits manually, e.g. with expand_limits(y=10.1), or use this trick to add an invisible layer with scaled up data,

    ggplot(mtcars, aes(x=as.factor(carb))) + 
        geom_bar() + 
        scale_y_continuous(expand = c(0,0)) +
        geom_blank(aes(y=1.1*..count..), stat="bin")
    

提交回复
热议问题