【问题标题】:How to change default aesthetics in ggplot?如何更改ggplot中的默认美学?
【发布时间】:2013-01-07 13:17:00
【问题描述】:

假设我希望 geom_point 默认使用圆圈 (pch=1) 而不是实心圆点 (pch=16)。您可以通过将shape 参数传递给geom_point 来更改标记的形状,例如

ggplot(diamonds, aes(depth, carat, colour=cut)) + geom_point(shape=1)
ggplot(diamonds, aes(depth, carat, colour=cut)) + geom_point(shape=16)

但我不知道如何更改默认行为。

【问题讨论】:

    标签: r ggplot2 default aesthetics


    【解决方案1】:

    几何(和统计)默认可以直接更新:

    update_geom_defaults("point", list(shape = 1))
    ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
    

    【讨论】:

      【解决方案2】:

      一种方法(虽然我不是很喜欢)是创建自己的 geom_point 函数。例如

      geom_point2 <- function(...) geom_point(shape = 1, ...)
      

      然后照常使用:

      ggplot(diamonds, aes(depth, carat, colour=cut)) + geom_point2()
      

      或者,如果您愿意,您可以覆盖函数geom_point()

      geom_point <- function(...) {
        ggplot2::geom_point(shape = 1, ...)
      }
      

      这可能被认为是不好的做法,但它确实有效。然后你不必改变你的绘图方式:

      ggplot(diamonds, aes(depth, carat, colour=cut)) + geom_point()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多