简体   繁体   中英

Difference between backticks and quotes in aes function in ggplot

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 :

在此处输入图片说明

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:

在此处输入图片说明

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

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