Use different font sizes for different portions of

2019-07-23 14:53发布

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??

标签: r ggplot2
1条回答
地球回转人心会变
2楼-- · 2019-07-23 15:31
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

查看更多
登录 后发表回答