首页 > 解决方案 > What's the meaning of ".." in R

问题描述

When plotting a contour plot, "..level.." is used in the code.

ggplot(faithful, aes(eruptions, waiting)) + stat_density2d(aes(colour = ..level..))

When plotting a barplot, "..count.." is used.

x <- sample(c('A','B','C','D'), size = 1000, replace= TRUE, prob = c(0.2,0.3,0.3,0.2))
y <- rnorm(1000) * 1000 
df = data.frame(x= x, y = y)
ggplot(data = df, aes(x = factor(x), y = ..count..))+ geom_bar(stat = 'count')

If we replace "..count.." with "count", an error occurs. So what is the difference?

标签: rggplot2

解决方案


..count..是一个标识符,就像 R 中的任何其他标识符一样,因为.它只是一个可以在 R 中的标识符中使用的普通字符——不像在其他编程语言中,它通常是一个特殊的运算符。请记住,在 R 中,您可以定义一个名为 的变量data.set

data.set <- data.frame(a=1:3, b=4:6)

变量名中的句点只是一个普通的标识符字符,如字母。

碰巧这..count..是一个标识符,它被选择ggplot来表示其特征之一,即基于计数的密度估计。


推荐阅读