ggplot中的双点 [英] double dots in a ggplot

查看:151
本文介绍了ggplot中的双点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到围绕密度的双点的文档。

  set.seed(1234)
df < - data.frame(cond = factor(rep(c(A,B),each = 200)),rating = c(rorm(200),rorm(200,mean = .8)))
print(head(df))
print(ggplot(df,aes(x = rating))+
geom_histogram(aes(y = .. density ..),#密度直方图而不是在y轴上计数
binwidth = .5,
color =black,fill =white)+
geom_density(alpha = .2,fill =#FF6666 )+
geom_vline(aes(xintercept = mean(rating,na.rm = T)),#忽略平均值
color =red,linetype =dashed,size = 1的NA值) )

你知道它们代表什么操作符?



编辑



我知道它在使用geom时的作用,我想知道它是什么它
例如,单点运算符被定义为

 > 。 
结构(as.list(match.call()[ - 1]),env = .env, class =quoted)
}
< environment:namespace:plyr>

如果我重新定义密度,那么 .. density .. 有不同的效果,所以它似乎是XX - > ..XX ..是一个运算符。我想知道它是如何定义的。

解决方案

与许多其他语言不同,在R中,点在标识符。在这种情况下, .. count .. 是一个标识符。然而,在 ggplot2 中有特殊的代码来检测这个模式,并去掉这些点。感觉不太可能真正的代码会使用像这样格式化的标识符,所以这是区分定义和计算美学的一种很好的方法。



相关的代码在最后 layer.r

 #确定是否计算美学
is_calculated_aes< - 函数(美学){
匹配< - \\.\\。( [A-ZA-Z ._] +)\\.\\。
stats< - rep(FALSE,length(aesthetics))
grepl(match,sapply(aesthetics,deparse))
}


strip_dots< - function(aesthetics){
match< - \\.\\。([a-zA-z ._] +)\\.\\ \\\。
字符串< - lapply(aesthetics,deparse)
字符串< - lapply(字符串,gsub,pattern = match,replacement =\\1)
lapply(字符串, function(x)parse(text = x)[[1]])
}

它在 map_statistic 函数中进一步用于上面。如果存在计算的美感,则使用另一个数据框(其中包含例如 count 列)的数据框。



单点只是 plyr 包中定义的另一个标识符。正如你所看到的,这是一个功能。


I can not find the documentation for the double dots around density

set.seed(1234)
df <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm(200, mean=.8)))
print(head(df))
print(ggplot(df, aes(x=rating)) + 
    geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                   binwidth=.5,
                   colour="black", fill="white") +
    geom_density(alpha=.2, fill="#FF6666") +
    geom_vline(aes(xintercept=mean(rating, na.rm=T)),   # Ignore NA values for mean
               color="red", linetype="dashed", size=1))

Do you know what operator they represent ?

Edit

I know what it does when used in a geom, I would like to know what it is. For instance, the single dot operator is defined as

> .
function (..., .env = parent.frame()) 
{
    structure(as.list(match.call()[-1]), env = .env, class = "quoted")
}
<environment: namespace:plyr>

If I redefine density, then ..density.. has a different effect, so it seems XX -> ..XX.. is an operator. I would like to find how it is defined.

解决方案

Unlike many other languages, in R, the dot is perfectly valid in identifiers. In this case, ..count.. is an identifier. However, there is special code in ggplot2 to detect this pattern, and to strip the dots. It feels unlikely that real code would use identifiers formatted like that, and so this is a neat way to distinguish between defined and calculated aesthetics.

The relevant code is at the end of layer.r:

# Determine if aesthetic is calculated
is_calculated_aes <- function(aesthetics) {
  match <- "\\.\\.([a-zA-z._]+)\\.\\."
  stats <- rep(FALSE, length(aesthetics))
  grepl(match, sapply(aesthetics, deparse))
}

# Strip dots from expressions
strip_dots <- function(aesthetics) {
  match <- "\\.\\.([a-zA-z._]+)\\.\\."
  strings <- lapply(aesthetics, deparse)
  strings <- lapply(strings, gsub, pattern = match, replacement = "\\1")
  lapply(strings, function(x) parse(text = x)[[1]]) 
}

It is used further up above in the map_statistic function. If a calculated aesthetic is present, another data frame (one that contains e.g. the count column) is used for the plot.

The single dot . is just another identifier, defined in the plyr package. As you can see, it is a function.

这篇关于ggplot中的双点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆