How to change default aesthetics in ggplot?

纵饮孤独 提交于 2019-12-08 21:14:52

问题


Let's say that I would prefer geom_point to use circles (pch=1) instead of solid dots (pch=16) by default. You can change the shape of the markers by passing a shape argument to geom_point, e.g.

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

but I can't figure out how to change the default behaviour.


回答1:


Geom (and stat) default can be updated directly:

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




回答2:


One way to do it (although I don't really like it) is to make your own geom_point function. E.g.

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

Then just use as normal:

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

Or if you want you can override the function geom_point():

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

This might be considered bad practice but it works. Then you don't have to change how you plot:

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


来源:https://stackoverflow.com/questions/14196804/how-to-change-default-aesthetics-in-ggplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!