在ggplot2中,当图表标题是一个变量时,如何更改绘图标题的字体大小?

7

我正在使用ggplot2生成散点图。我将标题制作成了一个变量,如何更改字体大小?代码如下:

library("ggplot2")
plotfunc <- function(x){
    x +
    geom_point() +
    geom_smooth(se = FALSE, method = "lm",  color = "blue", size = 1) +
    opts(title = plottitle,
           axis.title.x = theme_text(size = 8, colour = 'black'),
         axis.title.y = theme_text(size = 8, colour = 'black', angle = 90))
}

plottitle <- "This is Title"
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
plotfunc(p)

我已经尝试过

opts(title = plottitle (size = 10),...

但是出现了错误:

Error in opts(title = plottitle(size = 10),
axis.title.x = theme_text(size = 8,  : could not find function "plottitle"

它被识别为一个不符合我的要求的函数。我该怎么办?谢谢。


我尝试运行你的示例,但我没有你使用的“label”函数,另外你应该考虑添加一个require(ggplot2)。如果你的示例可以被复制,那么我们帮助你会更容易。 - Eric Fail
3个回答

8

如果 opts() 对你仍然有效,那么你正在使用较旧的ggplot2版本。新的命令是theme()。无论如何,你都不想将实际标题标签放入optstheme中 - 使用 labs()

plotfunc <- function(x) {
  x +
    geom_point() +
    geom_smooth(se = FALSE, method = "lm",  color = "blue", size = 1) +
    theme_bw() +
    theme(axis.title.x = element_text(size = 8, colour = 'black'),
          axis.title.y = element_text(size = 8, colour = 'black', angle = 90)) +
    labs(title='this', x='that', y='the other')
}

## plottitle <- "This is Title"
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
plotfunc(p)

enter image description here


2
请注意--如果您像上面那样在theme_bw()之前传递theme()的内容,那么您的size = 8选项将不起作用。 theme_bw()是一个预设,它将覆盖theme()的设置(在我的答案下面添加了一条注释)。尝试更改为size = 2,您会发现没有任何变化。在theme(...)之前放置theme_bw() +将起作用;它将首先应用预设,然后theme(...)参数将在适用的情况下覆盖theme_bw()的设置。 - Hendy

6
荒唐的晚回答,但我认为现有的答案没有解决实际问题:
引用: 我把标题变成了一个变量,如何改变字体大小?
这对我有效,并且在更新的ggplot语法(theme() vs. opts())中:
library(ggplot2)
plotfunc <- function(x){
    x +
    geom_point() +
    geom_smooth(se = FALSE, method = "lm",  color = "blue", size = 1) +
    labs(title = plottitle) +
    ### pay attention to the ordering of theme_bw() vs. theme()
    theme_bw() +
    theme(plot.title = element_text(size = 20),
          axis.title.x = element_text(size = 12),
          axis.title.y = element_text(size = 8))

}

plottitle <- "This is Title"
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
plotfunc(p)

我得到了以下内容:

ggplot theme(plot.title)

关于我的theme_bw()注释:尝试运行上述代码,但将theme_bw()放在最后,在theme(plot.title, ...)部分之后,就像Stephen Henderson的答案一样。您会注意到没有任何字体大小生效。这是因为theme_bw()是一个预设,如果在添加它们之后传递它,它将覆盖各种自定义theme()选项。
只是要注意的琐碎事情;我只学会了使用theme_bw()并且在摸索为什么其他theme()选项不起作用之前一直在撞头,才意识到这不是我的ggplot语法的问题,而是我的设置顺序。必须热爱编程 :)
此外,这里是可以传递给theme()的完整选项列表,供您参考可以进行哪些调整以及如何进行调整。

1

plottitle后面的下一个非空白字符处放置了一个"(",因此解释器认为它必须是一个函数。请尝试

  .... opts( title=plottile, size=10)

这是一长串温馨信息的列表:

Warning messages:
1: 'opts' is deprecated.
Use 'theme' instead.
See help("Deprecated") 
2: 'theme_text' is deprecated.
Use 'element_text' instead.
See help("Deprecated") 
3: 'theme_text' is deprecated.
Use 'element_text' instead.
See help("Deprecated") 
4: In opts(title = plottitle, axis.title.x = theme_text(size = 8, colour = "black"),  :
  Setting the plot title with opts(title="...") is deprecated. Use labs(title="...") or ggtitle("...") instead.

我尝试了opts(title=plottitle,size=10)+opts(axis.title.x=theme_text(size=8,colour='black'),axis.title.y=theme_text(size=8,colour='black',angle=90)),但是出现了错误:Error:"size" is not a valid theme element name - Autumn
又出现了一个错误,这不是你的代码可以预见到的。此外,当我运行代码时,我只会收到有关opts过时使用的警告,但我确实在图表上得到了那个标题。 - IRTFM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接