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

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

问题描述

我找不到关于密度周围双点的文档

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 ?

编辑

我知道它在 geom 中做什么,我想知道它是什么.例如,单点运算符定义为

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>

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

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.

推荐答案

与许多其他语言不同,在 R 中,点在标识符中完全有效.在这种情况下,..count.. 是一个标识符.但是,ggplot2 中有特殊的代码来检测这种模式,并去除点.感觉真正的代码不太可能使用这种格式的标识符,因此这是一种区分定义美学和计算美学的巧妙方法.

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.

相关代码在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]]) 
}

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

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.

单点. 只是另一个标识符,定义在plyr 包中.如您所见,它是一个函数.

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

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

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