7

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)

I tried

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

but there was an error:

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.

Autumn
  • 575
  • 1
  • 9
  • 20
  • I tried running your example, but I don't have the 'label' function you use, also you should consider adding an `require(ggplot2)`. If your example is actually reproducible it's much easier for us to help you. – Eric Fail Nov 27 '12 at 17:43

3 Answers3

8

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)

enter image description here

Tung
  • 26,371
  • 7
  • 91
  • 115
Stephen Henderson
  • 6,340
  • 3
  • 27
  • 33
  • 2
    FYI -- if you pass `theme()` stuff before `theme_bw()` as you do above, your `size = 8` options will have no effect. `theme_bw()` is a preset that will overwrite `theme()` settings (added a note about this in my answer below). Try changing to `size = 2` and you'll note nothing changes. Putting `theme_bw() +` before `theme(...)` will work; it will first apply the preset, and then `theme(...)` arguments will overwrite `theme_bw()`'s settings where applicable. – Hendy Apr 22 '15 at 19:07
6

Ridiculously late answer, but I didn't think the existing answers addressed the actual question:

I made the title into a variable, how can I change the font size?

This works for me and is in the updated ggplot syntax (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)

I get the following:

ggplot theme(plot.title)

A note about my theme_bw() comment: try running the above, but put theme_bw() last, after the theme(plot.title, ...) bit, as in Stephen Henderson's answer above. You'll note that none of the font sizes take effect. This is because theme_bw() is a preset which will overwrite various custom theme() options if you pass it after they are added.

Just a finnicky thing to watch out for; I've only learned it due to using theme_bw() a lot and banging my head against the wall trying to figure out why other theme() options weren't working before realizing it wasn't my ggplot syntax after all, but the order of my settings. Gotta love coding :)

Also, here's the full list of options you can pass to theme() as a reference for what you can tweak and how.

Hendy
  • 10,182
  • 15
  • 65
  • 71
1

You put a "(" as the next non-whitespace character after plottitle so the interpreter decided it must be a function. Try

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

This was the long list of warming messages:

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.
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I tried `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))` There was an error: `Error:"size" is not a valid theme element name` – Autumn Nov 27 '12 at 19:19
  • Yet another error and not one that was foreseeable from your code. Furthermore when I run the code I only get warnings about deprecated use of opts, but I do get that title on the plot. – IRTFM Nov 28 '12 at 03:16