Guilherme Parreira
Guilherme Parreira

Reputation: 1011

How do I define the legend position as a global option in ggplot2

I tried to set the legend position as a global option but it did not work. I was only able to set a theme as default, but not the legend position, as following:

theme_set(theme_bw()) # Defining the global theme
ggplot(data = iris, aes(x = Petal.Length, y=Sepal.Length, color = Species)) +
  geom_point()

enter image description here

I want the legend position to be at the bottom of the plot (as it will work for every plot, i.e. globally). How can I do it?

Cheers

Upvotes: 1

Views: 2218

Answers (2)

Andryas Waurzenczak
Andryas Waurzenczak

Reputation: 469

To change the default settings of ggplot2 you can do as follow:

new_theme <- theme_bw() %+replace% 
                           theme(legend.position = "bottom")

theme_set(new_theme)

obs: you can do this for any arg.

Upvotes: 5

zlipp
zlipp

Reputation: 801

It should be sufficient to just add + theme(legend.position = 'bottom') to your theme_set()


library(ggplot2)

theme_set(theme_bw() + theme(legend.position = 'bottom')) # Defining the global theme
ggplot(data = iris, aes(x = Petal.Length, y=Sepal.Length, color = Species)) +
  geom_point()

Upvotes: 4

Related Questions