In my work, I frequently display scatterplots using text labels. That's meant a plot()
command, followed by text()
. I used cex
to adjust to font size to what I wanted it very quickly.
I created a text scatterplot very quickly using qplot
. But I can't adjust the size fast. Here's a silly code example:
data(state)
qplot(Income, Population,
data=as.data.frame(state.x77),
geom=c("smooth", "text"), method="lm",
label=state.abb)
Whereas in the old days I'd do:
plot(xlim=range(Income), ylim=range(Population),
data=state.x77, type="n")
text(Income, Population, state.abb,
data=state.x77, cex=.5)
If I wanted the text size halved from what I saw at the defaults (oh, and I'd have to do a linear regression manually and add abline()
to get the regression line -- nice to do it all in one via ggplot2).
I know I can add a size adjustment with size, but it's not a relative size adjustment like I'm used to. Hadley tweeted me to say that size is measured in mm, which isn't fully intuitive to me. Since I often adjust the size of the plot, either in R or in LaTeX, an absolute scale isn't as useful to me.
I must be missing something really simple. What is it?
ggplot2 vs Base R Base R plots two vectors as x and y axes and allows modifications to that representation of data whereas ggplot2 derives graphics directly from the dataset. This allows faster fine-tuning of visualizations of data rather than representations of data stitched together in the Base R package1.
How can I change the default font size in ggplot2? Set base_size in the theme you're using, which is theme_gray() by default. The base font size is 11 pts by default. You can change it with the base_size argument in the theme you're using.
The base plotting paradigm is "ink on paper" whereas the lattice and ggplot paradigms are basically writing a program that uses the grid -package to accomplish the low-level output to the target graphics devices.
I think you are tyring to adjust the size of the text itself, not the x-axis, right?
Here's an approach using the ggplot()
command.
ggplot(data = as.data.frame(state.x77), aes(x = Income, y = Population)) +
geom_smooth(method = "lm", se = FALSE) +
geom_text(aes(label = state.abb), size = 2.5)
qp <- qplot(Income, Population,data=as.data.frame(state.x77),
geom=c("smooth","text"),
method="lm",
label=state.abb)
qp + opts(axis.text.x = theme_text(size = 5))
I think Chase is probably right about wanting points as "labels":
qp <- qplot(Income, Population,data=as.data.frame(state.x77),
geom="smooth",method="lm",label=state.abb)
qp + geom_text(aes(label = state.abb), size = 2.5)
If "text" is given in the geom argument to qplot the default size is used and then gets overwritten (or underwritten as it were in this case). Give Chase the check. (Edit: should make size 2.5)
Edit2: Took digging but I found the way to get ggplot2 to cough up some of its defaults: https://github.com/hadley/ggplot2/blob/master/R/geom-text.r
GeomText$new()$geom$default_aes
proto method (instantiated with ): function (.)
aes(colour = "black", size = 5, angle = 0, hjust = 0.5, vjust = 0.5,
alpha = 1)
There's got to be a better way....
qp <- qplot(Income, Population,data=as.data.frame(state.x77),
geom="smooth",method="lm",label=state.abb)
qp + geom_text(aes(label = state.abb, cex = 1.2))
Add cex
inside aes
will get what you want, as quoted from:
aes creates a list of unevaluated expressions. This function also performs partial name matching, converts color to colour, and old style R names to ggplot names (eg. pch to shape, cex to size)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With