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