简体   繁体   English

ggplot中es函数中反引号和引号之间的区别

[英]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. 我正在回答有关geom_histogram的初学者的问题,并且OP发布了一个使用反引号的示例。 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). 就连ggplot列表几乎相同,据我ggplot$mapping ,只有ggplot$mapping元素有所不同(好吧,这是一个大问题)。 I have googled about, but I don't see what is going on. 我已经在Google上搜索了一下,但是看不到发生了什么。

So here is the code: 所以这是代码:

This (quotes around Log Number in aes ): (在aes Log Number附近引用):

#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 ): 这种 (反引号周围的Log Numberaes ):

#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. 反斜杠是在R中表示非标准变量名称的标准方法。引号用于指示字符串。 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 ,如果您做了

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! 您会期望所有y值都为1。这是对的!

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). 除了代替上面的y = 1情况下的数字外,您给了它一个字符-对于离散的y比例(仅一个值),该字符被隐式转换为一个因数(仅一个级别)。 It doesn't matter if there's a column named "mpg" or not, because you've just passed aes() a value. 是否存在名为"mpg"的列都没有关系,因为您刚刚向aes()传递了一个值。 ggplot doesn't look for a column named mpg like it doesn't look for a column named 1 in the first example. ggplot不会在第一个示例中查找名为mpg的列,就不会在mpg中查找名为1的列。

With back ticks, you give ggplot something R recognizes as an object name , not just a value like 1 or "some string" . 随着反勾,你给ggplot东西[R识别为一个对象名称 ,而不是就像一个值1"some string" So ggplot does go looking for a column with that name. 因此, ggplot 确实会寻找具有该名称的列。

# 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. 虽然反滴答标记确实有效 ,并且通常在aes()设置常量也有效,但不建议二者都使用。 The preferred way to set constants is to set constants outside aes() . 设置常量的首选方法是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). 特别是,如果您尝试在aes()内做一些奇怪的事情(尤其是转换),则方面通常会出错或无法产生预期的结果。

# 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. 对于非标准的列名, aes_string()可以很好地工作,然后期望将美学映射引用为列名。 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. 如果正在编写创建ggplots且需要将列名作为参数的函数,这也是做事情的好方法。

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: 还有一个很好的例子,感谢@TheTime,它开始变得面目全非:

Eventually, ggplot needs to evaluate everything, which will be done with eval . 最终, ggplot需要评估所有内容,这将通过eval完成。 Consider the following: 考虑以下:

a <- 1

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM