我想我一定是误解line =了 中的论点theme()。根据 中的文档?theme,line =应该会影响图中的所有线元素。然而,改变颜色没有任何作用,而改变大小却起作用。
library(ggplot2)
ggplot(iris, aes(x = Sepal.Width, Petal.Length)) +
geom_point() +
geom_smooth() +
theme(line = element_line(color = "green", size = 5))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2022 年 8 月 15 日创建(v2.0.1)
我的问题有两个:
为什么更改颜色不会影响图中的任何线条?
为什么更改尺寸不会影响 中的线条元素geom_smooth()?
编辑:请参阅@Gregor Thomas 的答案以获取第 2 部分的答案。
这些元素确实如所宣传的那样继承,但是,它们可能有介于 childpanel.grid.major.x和 grandparent之间的父母line。例如theme_gray(),默认情况下的 就是这种情况。要测试这一点,您可以手动将所有主题元素设置为“空”元素。就像这样:
library(ggplot2)
my_theme <- theme_get() # Default theme gray
my_theme[] <- lapply(my_theme, function(elem) {
switch(
class(elem)[[1]],
"element_text" = element_text(),
"element_line" = element_line(),
"element_rect" = element_rect(),
elem
)
})
Run Code Online (Sandbox Code Playgroud)
因此,在消除了孩子和祖父母之间的所有步骤后,所有继承都应按其应有的方式进行。
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() +
my_theme +
theme(
line = element_line(colour = "green"),
axis.line = element_line() # was `element_blank()` by default
)
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2022 年 8 月 15 日创建(v2.0.1)