当标题是ggplot2中的变量时,如何更改绘图标题的字体大小? [英] How to change font size of plot title when the title is a variable in ggplot2?

查看:1432
本文介绍了当标题是ggplot2中的变量时,如何更改绘图标题的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using ggplot2 to generate a scatter plot. I made the title into a variable, how can I change the font size? The code is as the following:

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"

它被识别为不是我想要的功能. 我该怎么办?谢谢.

It was recognized as function which was not what I want. What should I do? Thanks.

推荐答案

如果 opts()仍然对您有用,则您使用的是ggplot2的旧版本.较新的命令是 theme().无论如何,您都不希望将实际标题标签放入 opts theme 中-使用 labs()

If opts() still works for you then you are using an old version of ggplot2. The newer command is theme(). In any case you don't want to put the actual title label into opts or theme -- use 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)

这篇关于当标题是ggplot2中的变量时,如何更改绘图标题的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆