Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between backticks and quotes in aes function in ggplot

Tags:

r

ggplot2

Ok, this is kind of an odd one. I was answering a question for a beginner around geom_histogram, and the OP posted an example using backticks. He neglected to add data so I made it up, and then found an answer, not even noticing the backticks. But another (actually more elegant) answer was posted without backticks. It did not really work, but it worked a lot better with the backticks.

But now I am puzzled. I don't see why there should have been a difference at all. Even the ggplot list is almost identical, only the ggplot$mapping element is different as far as I can see (ok, that is a biggie). I have googled about, but I don't see what is going on.

So here is the code:

This (quotes around Log Number in aes):

#Generate some data
lon <- log(rnorm(1000, exp(6)))
state <- sample(c("c", "l", "t"), 1000, replace = T)
d <- data.frame(lon, state)
names(d) <- c("Log Number", "state")

# Plot it
gpsq <- ggplot(d, aes(x = 'Log Number', fill = state)) + geom_histogram()
print(gpsq)

yields this:

enter image description here

But this (backticks around Log Number in aes):

#Generate some data
lon <- log(rnorm(1000, exp(6)))
state <- sample(c("c", "l", "t"), 1000, replace = T)
d <- data.frame(lon, state)
names(d) <- c("Log Number", "state")

# Plot it
gpsq <- ggplot(d, aes(x = `Log Number`, fill = state)) + geom_histogram()
print(gpsq)

more correctly yields this:

enter image description here

like image 287
Mike Wise Avatar asked Nov 07 '15 00:11

Mike Wise


People also ask

How does AES work in Ggplot?

aes() is a quoting function. This means that its inputs are quoted to be evaluated in the context of the data. This makes it easy to work with variables from the data frame because you can name those directly. The flip side is that you have to use quasiquotation to program with aes() .

What do backticks mean in R?

backticks are used to indicate that the text between the backticks should be executed as a command. '['(women, 1:5, 1) "["(women, 1:5, 1) Copy [1] 58 59 60 61 62 [1] 58 59 60 61 62. It also turns out that ( is a function as well, which was what I surmised in my previous post.

What does AES do in R?

In R, the aes() function is often used within other graphing elements to specify the desired aesthetics. The aes() function can be used in a global manner (applying to all of the graph's elements) by nesting within ggplot() .


1 Answers

Back ticks are the standard way of denoting a non-standard variable name in R. Quotes are used to indicate a string. Example:

`bad name` = 1
`bad name`
# [1] 1

This doesn't work with quotes.

"bad name" = 1
"bad name"
# [1] "bad name"

Generally, you shouldn't use these strange, non-standard names. But, if you ever have to, that's the way to do it. You can do pretty much anything,

`really-bad^name+violating*all()/[kinds] <- of == rules&!|` = 1
# works just fine

but that doesn't mean you should.


When it comes to ggplot, if you did

ggplot(mtcars, aes(x = wt, y = 1)) + geom_point()

you would expect that all the y-values would be 1. And you'd be right!

With a quoted string it's just the same:

ggplot(mtcars, aes(x = wt, y = "mpg")) + geom_point()

except instead of a numeric as in the y = 1 case above, you've given it a character - which is implicitly converted to a factor (with only one level) for a discrete y scale (with only one value). It doesn't matter if there's a column named "mpg" or not, because you've just passed aes() a value. ggplot doesn't look for a column named mpg like it doesn't look for a column named 1 in the first example.

With back ticks, you give ggplot something R recognizes as an object name, not just a value like 1 or "some string". So ggplot does go looking for a column with that name.

# both of these work
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
ggplot(mtcars, aes(x = wt, y = `mpg`)) + geom_point()

While back ticks do work, and setting constants inside aes() usually does work, neither of these are very recommended. The preferred way to set constants is to set constants outside aes(). This is the only way to guarantee everything will work nicely in more complicated plots. Facets, in particular, often have errors or don't produce expected results if you try to do weird stuff inside aes() (especially transformations).

# better than above, set a constant outside of `aes()`
# Here I set y as a constant which is a bit unusual
ggplot(mtcars, aes(x = wt)) + geom_point(y = 1)
# aesthetics that are more commonly set to constants are
# size, color, fill, etc.

For non-standard column names, aes_string() works well, and then it expects the aesthetic mappings to be quoted column names. This also is a good way to do things if you are writing a function that creates ggplots and needs to take column names as arguments.

ggplot(mtcars, aes_string(x = "wt", y = "mpg")) + geom_point()
# or, in a variable
my_y_column = "mpg"
ggplot(mtcars, aes_string(x = "wt", y = my_y_column)) + geom_point()

One more nice example, beginning to look under-the-hood, thanks to @TheTime:

Eventually, ggplot needs to evaluate everything, which will be done with eval. Consider the following:

a <- 1

eval(parse(text="a"))
# [1] 1

eval(parse(text='"a"'))
# [1] "a"

eval(parse(text="`a`"))
# [1] 1
like image 89
Gregor Thomas Avatar answered Sep 28 '22 03:09

Gregor Thomas