强制原点从0开始,而新ggplot主题中的数据和x轴之间没有空白 [英] Force the origin to start at 0 without margin between data and x axis in new ggplot theme

查看:291
本文介绍了强制原点从0开始,而新ggplot主题中的数据和x轴之间没有空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此问题强制起源从0开始我想将此实现到新创建的主题中,而不仅仅是将其设置为默认设置.

Based on this question Force the origin to start at 0 I want to implement this into a new created theme and not just as a default setting.

data(iris)
ggplot(iris, aes(x = Species)) + 
  geom_bar() +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05)))

如何在不对每个图进行定义的情况下做到这一点?

How can I do this without defining it every single plot?

推荐答案

据我所知,这不可能通过theme()进行.但是您可以像这样在scale_y_continuous周围定义包装器

As far as I know this not possible via theme(). But you can define a wrapper around scale_y_continuous like so

library(ggplot2)

data(iris)

scale_y_origin <- function(...) {
  scale_y_continuous(expand = expansion(mult = c(0, 0.05)), ...)
}

ggplot(iris, aes(x = Species)) + 
  geom_bar() +
  scale_y_origin()

这篇关于强制原点从0开始,而新ggplot主题中的数据和x轴之间没有空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆