7

Consider the following graph:

require(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() +
  labs(title = 'Iris[small font]' ) +
  theme_classic()

enter image description here

The Left graph is the code output, the right graph shows the desired result, I used Adobe Illustrator for that

The question is, if it is possible to change the font size in line, in this example the "[small font]" label in the title, but of course it is a general question also regarding other labels such as for the axes and legend etc.

Obviously, the font size is set with theme(). However, there might be a way setting a "relative font size", e.g. using rel() and using this somehow with a labeller function??

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • 1
    Not directly with ggplot2. Take a look at [`gridtext`](https://github.com/clauswilke/gridtext). Using it in conjunction with one of the plethora of grob-hacking hacks on SO to substitute the title grob for something created by `gridtext` may help you get the title the way you want. – hrbrmstr Oct 17 '18 at 10:53
  • 1
    Slightly modified the title for better google search results – hrbrmstr Oct 17 '18 at 11:39
  • Thanks for the suggestion! But I was actually asking if there was a more general way of changing the text size in line, also usable for axes etc. But I guess this is too general. I like your solution for my specific title problem, and I will accept probably, but will still wait a bit longer :) – tjebo Oct 17 '18 at 11:42
  • 1
    Feel free to wait, but all ggplot2 text geoms and plot annotation grobs use single text grob elements (look a the source as noted in the comment on my answer). grid text grobs have [single aesthetic mappings](https://www.rdocumentation.org/packages/grid/versions/3.5.1/topics/grid.text). – hrbrmstr Oct 17 '18 at 11:46

2 Answers2

10
library(grid)
library(gridtext) # devtools::install_github("clauswilke/gridtext")
library(gridExtra)
library(ggplot2)

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point() +
  labs(title = "Replace-able") -> gg

gb <- ggplot_build(gg)
gt <- ggplot_gtable(gb)

title <- "<span style='font-size:20'>Iris </span><span style='font-size:12'>[some text]</span>"
tg <- rich_text_grob(title, x = unit(0, "lines"), y = unit(2, "lines"))

gt$grobs[[16]] <- tg

grid.newpage()
grid.draw(gt)

enter image description here

Then, there's:

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point() -> gg

title <- "<span style='font-size:20'>Iris </span><span style='font-size:12'>[some text]</span>"
tg <- rich_text_grob(title, x = unit(2, "lines"), y = unit(2, "lines"))

grid.newpage()
grid.draw(
  arrangeGrob(tg, gg, heights=c(0.1, 0.8))
)

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • 1
    how about the same person having another idea? :-) Really, though, this is not native ggplot2 functionality and likely not going to be for a while, vis-a-vis https://github.com/tidyverse/ggplot2/blob/7ed8930c1385935e875465efaa7d72a552d08612/R/theme-elements.r#L216-L218 – hrbrmstr Oct 17 '18 at 11:36
  • I shld have pointed out the ability to find `[[16]]` by name. When I get some time later I'll modify the code. – hrbrmstr Oct 17 '18 at 14:22
4

You can avoid the fiddling with grid grobs if you use the package ggtext instead.

# this requires the current development versions of ggplot2 and ggtext
# remotes::install_github("tidyverse/ggplot2")
# remotes::install_github("clauswilke/ggtext")

library(ggplot2)
library(ggtext)

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point() +
  ggtitle("<span style='font-size:20pt'>Iris </span><span style='font-size:12pt'>[some text]</span>") +
  theme(
    plot.title = element_markdown()
  )

Created on 2019-12-03 by the reprex package (v0.3.0)

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104