Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Defaults for geoms and scales ggplot2

Tags:

Theming for ggplot2 makes it quite easy to relegate the need for multiple or repetitive + opt()... lines. However, I would like to know if there is a way to define defaults for geoms and scale colors. Instead of having to write ...+ scale_fill_manual() for each plot, I'd like to be able to set it and forget it. Similarly, I'd like to be able to set geom options so I don't have to retype (or forget to retype) things like geom_text(...,size=3,color="white")

Update:

For scales, it seems at some point that there was a method:

set_default_scale("colour", "discrete", "grey")

But this function doesn't seem to exist in the most recent version of ggplot2.

like image 930
Brandon Bertelsen Avatar asked Mar 30 '12 13:03

Brandon Bertelsen


People also ask

How do I change the Y axis to log scales ggplot2?

This can be done easily using the ggplot2 functions scale_x_continuous() and scale_y_continuous(), which make it possible to set log2 or log10 axis scale. An other possibility is the function scale_x_log10() and scale_y_log10(), which transform, respectively, the x and y axis scales into a log scale: base 10.

What is ggplot2 scale?

Scales in ggplot2 control the mapping from data to aesthetics. They take your data and turn it into something that you can see, like size, colour, position or shape. They also provide the tools that let you interpret the plot: the axes and legends.

Are scales part of ggplot2?

The scales packages provides the internal scaling infrastructure used by ggplot2, and gives you tools to override the default breaks, labels, transformations and palettes.

What does Geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.


2 Answers

There is another method for this now. You can essentially overwrite any aesthetics scale, for example:

scale_colour_discrete <- function(...) scale_colour_brewer(..., palette="Set2")
scale_fill_discrete <- function(...) scale_fill_brewer(... , palette="Set2")

Now, your aesthetics will be coloured or filled following that behaviour.'

As per: https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/w0Tl0T_U9dI

With respect to defaults to geoms, you can use update_geom_defaults, for example:

update_geom_defaults("line",   list(size = 2))
like image 50
Brandon Bertelsen Avatar answered Oct 06 '22 04:10

Brandon Bertelsen


I can't think of anything useful for the geoms, but for the scales, one option would be to use the fact that components of ggplots are all simply R objects that can be saved, stored and reassigned like any other.

So you could perhaps create your own collection of "default" versions of many scales, like:

sfmDefault <- scale_fill_manual(...)
scmDefault <- scale_colour_manual(...)

etc. with your desired default values. Put them in your .RProfile or wherever and use them as needed.

like image 36
joran Avatar answered Oct 06 '22 02:10

joran