Reputation: 1791
R
, ggplot2
question.
?ggplot2::rel()
says
rel() is used to specify sizes relative to the parent.
What is the "parent" exactly? For example, I'd love to set the size of my plot title to rel(5)
. What is the width of my title in inches exactly?
I noticed there are two "units" that I believe somehow are used as a relevant size in ggplot2, .pt
. I think there is some relation between rel()
and .pt
. .pt
equals to 2.845276
. Why?? And 2.845276 of what? Pixels?
Upvotes: 5
Views: 1440
Reputation: 77106
with regards 2.845276
,
all.equal(convertUnit(unit(1,"mm"),"pt", valueOnly = TRUE), ggplot2::.pt)
Upvotes: 1
Reputation: 132706
The parents are defined in help("theme")
. Note how for most arguments the documentation says "inherits from ...". This is object oriented programming.
E.g., axis.text
is the parent of axis.text.x
:
library(ggplot2)
library(gridExtra)
DF <- data.frame(x = 1, y = 2)
p1 <- ggplot(DF, aes(x, y)) + geom_point()
p2 <- p1 + theme(axis.text.x = element_text(size = rel(2)))
p3 <- p2 + theme(axis.text = element_text(size = 5))
grid.arrange(p1, p2, p3, ncol = 1)
Upvotes: 4
Reputation: 4993
The parent element is the graph itself as drawn by the current theme. You can use rel()
to scale anything relative to the parent object which is not a part of the data (titles, axes and such).
It is specifically used to scale things relative to the rest of the theme for the current plot.
As you might expect with rel being short for relative, there is no absolute size for it, not inches or centimeters. But you can use it to scale your plot's title as follows:
g + theme(plot.title = element_text(size = rel(5)))
Where g is the plot you are adding things onto.
This tells ggplot to scale the text in your plot title to scale it up 5 relative to rest of the theme. You can also scale it down using decimals. The numbers are a bit weird, like with line sizes or glyph sizes, the best thing is to try a few to see what gets you closest to where you want to be!
Upvotes: 1