R 如何在函数中应用ggtheme并在图形上应用标签?

R 如何在函数中应用ggtheme并在图形上应用标签?,r,ggplot2,label,R,Ggplot2,Label,我希望将ggthemes库中的主题应用于函数内部的ggplot。然而,ggthemes似乎阻止了标签的显示。为什么会发生这种行为 library(tidyverse) library(ggthemes) d <- sample_n(diamonds, size = 50) p <- ggplot(d, aes(carat, price)) + geom_point() f1 <- function(g, title) { g %+% theme_few() %+% gg

我希望将
ggthemes
库中的主题应用于函数内部的
ggplot
。然而,
ggthemes
似乎阻止了标签的显示。为什么会发生这种行为

library(tidyverse)
library(ggthemes)

d <- sample_n(diamonds, size = 50)
p <- ggplot(d, aes(carat, price)) + geom_point()

f1 <- function(g, title) {
  g %+% theme_few() %+% ggtitle(title)
}

f2 <- function(g, title) {
  g %+% ggtitle(title)
}

f1(p, "testing")
f2(p, "testing")
库(tidyverse)
图书馆(主题)

d我相信
plot.title
在使用
theme\u now()
时会被删除。您只需将其添加回相同的函数:

f3 <- function(g, title) {
  g %+% ggtitle(title) %+% theme_few() %+%
    theme(plot.title = element_text(color="black"))
}
问题起因: 实际上,它看起来像是
plot.title
没有被删除,真正的问题影响的不仅仅是标题。它看起来像是
ggthemes
从包数据
ggthemes\u数据
继承了所有颜色。但是,他们最近重命名了
ggthemes\u data$now
(从
black
black
)的颜色,但未能更新函数,这导致
themes\u now()
中的所有颜色元素现在被读取为
NA
,从而从主题中删除了该元素


因此,在标题的情况下,
theme\u-now
在调用
ggthemes\u数据$now$dark[“black”]
时,它应该调用
ggthemes\u数据$now$dark[“black”]
。看起来这个问题会影响这个特定函数中的所有颜色元素,所以我暂时不使用这个版本的
ggthemes
。我很惊讶这个疏忽没有被发现

我可能刚刚意识到这可能就是主题。也许,最近更新的
ggthemes
包改变了这个主题?我当前的
ggthemes
版本是3.5.0这很有意义!非常感谢。我本来打算打开一个github问题,但看起来这个问题已经打开了:您可以回滚到版本3.4.2或安装github版本,该版本应该可以修复它
f3(p, "testing")