1

This is my first question on stackoverflow so please correct me if the question is unclear.

I would like to assign geom attributes for ggplot2 to a variable for reuse in multiple plots. For example, let's say I want to assign the attributes of size and shape to a variable to resuse in plotting data other than mtcars.

This code works, but if I have a lot of plots I don't want to keep re-entering the size and shape attributes.

ggplot(mtcars) +
  geom_point(aes(x = wt, 
             y = mpg),
         size = 5,
         shape = 21
         )

How should I assign a variable (eg size.shape) these attributes so that I can use it in the below code to produce the same plot?

ggplot(mtcars) +
  geom_point(aes(x = wt,
             y = mpg),
         size.shape
         )
3
  • 2
    You could do size.shape <- list(size = 5, shape = 21); ggplot(mtcars) + do.call(geom_point, c(list(aes(x = wt, y = mpg)), size.shape)). Commented Apr 23, 2016 at 20:33
  • might be a good use-case for aes_string() Commented Apr 24, 2016 at 1:52
  • Thanks guys! do.call and Stibu's answer on changing the defaults permits solving the problem in a variety of ways. Awesome! Commented Apr 24, 2016 at 23:02

1 Answer 1

1

If you always want to use the same values for size and shape (or other aesthetics), you could use update_geom_defaults() to set the default values to other values:

update_geom_defaults("point", list(size = 5, shape = 21))

These will then be used whenever you do not specifically give values for the aesthetics.

Example

The plot you create with the usual default settings looks as follows:

ggplot(mtcars) + geom_point(aes(x = wt, y = mpg))

enter image description here

But when you reset the defaults for size and shape, it looks differently:

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

enter image description here

As you can see, the actual plot is done with the same code as before, but the result is different because you changed the default values for size and shape. Of course, you can still produce plots with any value for these aesthetics, by simply providing values in geom_point():

ggplot(mtcars) + geom_point(aes(x = wt, y = mpg), size = 2, shape = 2)

enter image description here

Note that the defaults are given by geom, which means that only geom_point() is affected.

This solution is convenient, if there is only one set of values for size and shape that you want to use. If you have several sets of values that you want to be able to pick from when creating a plot, then you might be better off with something along the lines of the comment by lukeA.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Stibu! This is really going to come in handy! I tried to give it an upvote, but I'm so new I don't have enough reputation points yet. Until then, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.