简体   繁体   中英

cex equivalent in ggplot2

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?

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)

  1. http://docs.ggplot2.org/current/aes.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM