20

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?

user438383
  • 5,716
  • 8
  • 28
  • 43
bshor
  • 4,859
  • 8
  • 24
  • 33

3 Answers3

12

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)
Chase
  • 67,710
  • 18
  • 144
  • 161
  • Yes, I want to adjust the text itself. This code does make them smaller. But is 2.5 an absolute size, or a relative one? – bshor Dec 25 '10 at 01:08
  • @bshor - It appears they are absolute sizes, though it seems that a bit of trial and error would yield the appropriate size font for your interactive sessions? Similarly, you should be able to scale this pretty easily to the size output you need if you are using `ggsave()` or `png()` et al, no? – Chase Dec 25 '10 at 05:57
6
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....

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • But that makes it 5pt right? To make it a relative size, you'd need to get the regular font size from the plot's current theme. – Daniel Dickison Dec 24 '10 at 20:55
  • I was choosing the size=5 as 1/2 of the default (= 10). If you wanted to put it in as 0.5*10 that should be OK. Take a look at the default arguments of theme_text. – IRTFM Dec 24 '10 at 21:35
  • Isn't the default 5, as you say? But, anyway, this is the confusing part coming from base graphics. Is the default, or any adjustment, absolute sizes, or relative to the size of the plot? – bshor Dec 25 '10 at 01:12
  • It's 10 for the axis labels (see the code for theme_text) and 5 for the geom_text (see above). – IRTFM Dec 25 '10 at 04:34
  • For others stumbling on this, see the [updated documentation](http://docs.ggplot2.org/0.9.3/theme.html) (0.9.3 at present) for the revised specification. `opts()` changed to `theme()`, and `theme_text()` is now `element_text()` – Hendy Jul 24 '13 at 19:25
3
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)

  1. http://docs.ggplot2.org/current/aes.html
Tal Galili
  • 24,605
  • 44
  • 129
  • 187
Puriney
  • 2,417
  • 3
  • 20
  • 25