Difference Between Backticks and Quotes in Aes Function in Ggplot

Difference between backticks and quotes in aes function in ggplot

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

How to convert quotes of character elements to be surrounded by backticks instead of quotes in R

You cannot have a vector that contain backtick quoted names. Since you said you are using for loop, you can transform each name to contain the backticks within the for loop using as.name:

as.name(cols[1])
`big creek`


lapply(cols, as.name)
[[1]]
`big creek`

[[2]]
`gage creek`

[[3]]
`garvey creek`

[[4]]
`larches creek`

[[5]]
`little usable`

Ggplot2 and tibble plotting geom_point not not detecting dates/factors

Try this:

library(tidyverse)
a %>% ggplot(aes(year, visits))+ geom_point(aes(color=icd_group1))

Sample Image

How to refer to a variable name with spaces?

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.



Related Topics



Leave a reply



Submit