如何在ggplot2中将图例位置定义为全局选项

时间:2017-10-31 20:28:11

标签: r ggplot2

我尝试将图例位置设置为全局选项,但它不起作用。我只能将主题设置为默认值,而不是图例位置,如下所示:

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

我希望图例位置位于图的底部(因为它适用于每个图,即全局图)。我该怎么办?

干杯

2 个答案:

答案 0 :(得分:3)

要更改ggplot2的默认设置,您可以执行以下操作:

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

theme_set(new_theme)

obs:你可以为任何arg做这个。

答案 1 :(得分:2)

只需将+ theme(legend.position = 'bottom')添加到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()