30

In ggplot2, how do I refer to a variable name with spaces?

Why do qplot() and ggplot() break when used on variable names with quotes?

For example, this works:

qplot(x,y,data=a)

But this does not:

qplot("x","y",data=a)

I ask because I often have data matrices with spaces in the name. Eg, "State Income". ggplot2 needs data frames; ok, I can convert. So I'd want to try something like:

qplot("State Income","State Ideology",data=as.data.frame(a.matrix))

That fails.

Whereas in base R graphics, I'd do:

plot(a.matrix[,"State Income"],a.matrix[,"State Ideology"])

Which would work.

Any ideas?

isomorphismes
  • 8,233
  • 9
  • 59
  • 70
bshor
  • 4,859
  • 8
  • 24
  • 33

3 Answers3

29

Answer: because 'x' and 'y' are considered a length-one character vector, not a variable name. Here you discover why it is not smart to use variable names with spaces in R. Or any other programming language for that matter.

To refer to variable names with spaces, you can use either hadleys solution

a.matrix <- matrix(rep(1:10,3),ncol=3)
colnames(a.matrix) <- c("a name","another name","a third name")

qplot(`a name`, `another name`,data=as.data.frame(a.matrix)) # backticks!

or the more formal

qplot(get('a name'), get('another name'),data=as.data.frame(a.matrix))

The latter can be used in constructs where you pass the name of a variable as a string in eg a loop construct :

for (i in c("another name","a third name")){
    print(qplot(get(i),get("a name"),
      data=as.data.frame(a.matrix),xlab=i,ylab="a name"))
    Sys.sleep(5)
}

Still, the best solution is not to use variable names with spaces.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • Thanks; this works great, and the tip about "get" and looping is neat. My only quibble is that names with spaces, while more cumbersome to work with, are often more legible, and often can be passed on to LaTeX code without renaming. – bshor Dec 29 '10 at 16:00
5

Using get is not more "formal", actually I would argue the opposite. As the R help says (help("`")), you can almost always use a variable name that contains spaces, provided it's quoted. (Normally, with a backtick, as already suggested.)

Davor Cubranic
  • 1,051
  • 10
  • 16
3

Something similar was asked on ggplot2 mailing list and Mehmet Gültaş linked to this post. Another way of using strings to construct your ggplot call is through the aes_strings function. Note that you still have to put backticks inside the quotes for the thing to work for variables with spaces.

library(ggplot2)

names(mtcars)[1] <- "em pi dzi"

ggplot(mtcars, aes_string(x = "cyl", y = "`em pi dzi`")) +
  theme_bw() +
  geom_jitter()
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197