ggplot中aes函数中反引号和引号之间的区别 [英] Difference between backticks and quotes in aes function in ggplot
问题描述
geom_histogram
的初学者的问题,OP公布了一个使用反引号的例子。他忽略了添加数据以便我补充,然后找到了答案,甚至没有注意到反引号。但另一个(实际上更优雅)的答案没有反引号发布。它并没有真正的工作,但反引号的效果更好。 但现在我感到困惑。我不明白为什么应该有所不同。即使ggplot列表几乎相同,只要 ggplot $ mapping
元素与我所见到的不同(就是说,这是一个biggie)。我已经搜索了一下,但我不明白发生了什么。
所以这里是代码:
< (在
aes
中)引用 Log Number
): b $ b
#生成一些数据
lon <-log(rnorm(1000,exp(6)))
state< - 样本(c(c,l,t),1000,replace = T)
d < - data.frame(lon,state)
名称(d)< c(Log Number,state)
#绘制
gpsq < - ggplot(d,aes(x ='Log Number',fill = state))+ geom_histogram ()
print(gpsq)
产生此:
但 (在 aes
):
<$ p $ 生成一些数据
lon <-log(rnorm(1000,exp(6)))
state < - sampl e(c(c,l,t),1000,replace = T)
d < - data.frame(lon,state)
名称(d)< - c (Log Number,state)
#绘制
gpsq < - ggplot(d,aes(x =`Log Number`,fill = state))+ geom_histogram )
print(gpsq)
更正确地产生这种结果:
表示R中的非标准变量名。引号用于表示字符串。例子:
`bad name` = 1
`坏名字`
#[1] 1
这不适用于引号。
坏名字= 1
坏名字
#[1]坏名字
通常,您不应该使用这些奇怪的非标准名称。但是,如果你需要,那就是这样做的方法。您可以完成任何事情,
`really-bad ^ name +违反* all() / [种类]< - of == rules&!|`= 1
#可以正常工作
但这并不意味着你应 。
当涉及到 ggplot
,如果您这样做了
ggplot(mtcars,aes(x = wt,y = 1))+ geom_point()
你会期望所有的y值会是的,你会是正确的!
使用带引号的字符串,它是一样的:
<$ p $ ($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b
除了上面的 y = 1
情况下的数字外,您已给它一个字符 - 将其隐式转换为一个因子(只有一个级别)离散y规模(只有一个值)。不管是否有名为mpg
的列,因为您刚刚通过了 aes()
一个值。 ggplot
不会查找名为 mpg
的列,例如它不会查找名为<$ c $的列在第一个例子中,c> 1 。
使用back ticks,您可以给 ggplot
R认定为一个对象名称,而不仅仅是像 1
或某些字符串
。因此, ggplot
不会去寻找具有该名称的列。
#这两种工作都是
ggplot(mtcars,aes(x = wt,y = mpg))+ geom_point()
ggplot(mtcars,aes(x = wt,y = `mpg`))+ geom_point()
在 aes()
中设置常量通常可以工作,这些都不是很推荐的。设置常量的首选方式是在 aes()
之外设置常量。这是保证一切在更复杂的情节中都能正常工作的唯一方法。如果您尝试在 aes()
(特别是转换)中做奇怪的事情,那么特别常常会出现错误或无法产生预期结果。
#比上面更好,在`aes()`
#之外设置一个常量在这里,我将y设置为常量,这有点不寻常
ggplot(mtcars,aes(x = wt))+ geom_point(y = 1)
#更常见设置为常数的美学是
#大小,颜色,填充等
对于非标准列名称, aes_string()
运作良好,然后它期望审美映射被引用列名称。如果您正在编写一个创建ggplots并需要将列名作为参数的函数,这也是一种很好的方法。
ggplot(mtcars,aes_string(x =wt,y =mpg))+ geom_point()
#或变量
my_y_column =mpg
ggplot(mtcars ,aes_string(x =wt,y = my_y_column))+ geom_point()
还有一个很好的例子,开始看起来很不错,感谢@TheTime:
最后, ggplot
需要评估一切,这将通过 eval
完成。考虑以下内容: a < - 1
eval(parse(text =a ))
#[1] 1
eval(parse(text ='a'))
#[1]a
eval(parse(text =`a))
#[1] 1
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
这篇关于ggplot中aes函数中反引号和引号之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!