count0 count0 - 4 months ago 14
R Question

What are good ways to preset plotting options for ggplot in R

Im having a bunch of lengthy repetitive ggplot2 instructions throughout a file. So far i've been using variables control preset values keeping graphs uniform, having the configuration in one spot (e.g. to change the color of all graphs).

What i'm really looking for is a good way to preset those instructions so i don't have to write the statements all over. As an example, i define somewhere top in the file:

OPTAX=theme_text(angle=30, hjust=1, vjust=1, size=8)

Where the plot statements is, i use

aes(x=Topics, y=value, fill=variable)) +

I would like to avoid writing the
part and write something like

... + opts(MYOPTS) + ...

or avoid the opts statement at all and write

... + ALLMYOPTS + ...

so all my options are predefined in one statement.

As a second thing it would be good to override the statement. Something like

opts(MYOPTS, axis.text.x=theme_text(angle=60)

would be great, so i can keep the preset but still use custom options.


This will work:

ALLMYOPTS <- opts(axis.text.x = theme_text(angle=30, hjust=1, vjust=1, size=8))