我正处于学习如何扩展ggplot2的早期阶段。我想创建一个自定义geom和相关的stat。我的出发点是格列奈特。此外,我还从这和这中受益。我正在努力拼凑一个模板来教我自己和其他人。
主要问题:
在我的函数calculate_shadows() params$anchor 中的所需的参数是 NULL**.我怎样才能访问它?**
下面描述的目标仅仅是为了学习如何创建自定义的stat和geom函数,这并不是一个真正的目标:正如您可以从屏幕截图中看到的那样,我确实知道如何利用ggplot2的力量来生成图形。
geom将读取数据,而对于所提供的变量,("x", "y")将绘制(因为缺少更好的单词) shadows:默认y=0处的水平线min(x)--max(x)和默认x=0处的垂直线min(y)--max(y)。如果提供了选项,这些“锚”可以更改,例如,如果用户提供x = 35, y = 1,则在拦截y = 1处绘制水平线,而在拦截x = 35处绘制垂直线。用法:
库(Ggplot2) ggplot(data =mtcar,aes(x = mpg,y= wt)) + geom_point() + geom_shadows(x = 35,y= 1)

stat将读取数据,对于所提供的变量,("x", "y")将根据stat的值计算shadows。例如,通过传递stat = "identity",阴影将计算数据的最小值和最大值(如geom_shadows所做的)。但通过stat = "quartile",阴影将计算第一和第三四分位数。更广泛地说,我们可以传递一个函数,比如带有参数args = list(probs = c(0.10, 0.90), type = 6)的args = list(probs = c(0.10, 0.90), type = 6)函数,以使用第10和第90百分位数和类型6的分位数方法来计算阴影。
geom_point() + stat_shadows(stat =“四分位数”)

不幸的是,我对扩展ggplot2的不熟悉使我无法达到我的目标。这些情节是用geom_segment“伪造”的。基于上面提到的教程和讨论,以及检查现有代码(如stat-qq或stat-smooth ),我为这个目标构建了一个基本的体系结构。它必须包含许多错误,我将感谢您的指导。另外,请注意,这两种方法都很好:geom_shadows(anchor = c(35, 1))或geom_shadows(x = 35, y = 1)。
下面是我的努力。首先,geom-shadows.r定义geom_shadows()。第二,stat-shadows.r定义stat_shadows()。代码不能按原样工作。但是如果我执行它的内容,它确实会产生所需的统计数据。为了清晰起见,我已经删除了stat_shadows()中的大多数计算,比如四分位数,来关注本质。布局上有明显的错误吗?
geom-shadows.r
#' documentation ought to be here
geom_shadows <- function(
mapping = NULL,
data = NULL,
stat = "shadows",
position = "identity",
...,
anchor = list(x = 0, y = 0),
shadows = list("x", "y"),
type = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomShadows,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
anchor = anchor,
shadows = shadows,
type = type,
na.rm = na.rm,
...
)
)
}
GeomShadows <- ggproto("GeomShadows", Geom,
# set up the data, e.g. remove missing data
setup_data = function(data, params) {
data
},
# set up the parameters, e.g. supply warnings for incorrect input
setup_params = function(data, params) {
params
},
draw_group = function(data, panel_params, coord, anchor, shadows, type) {
# draw_group uses stats returned by compute_group
# set common aesthetics
geom_aes <- list(
alpha = data$alpha,
colour = data$color,
size = data$size,
linetype = data$linetype,
fill = alpha(data$fill, data$alpha),
group = data$group
)
# merge aesthetics with data calculated in setup_data
geom_stats <- new_data_frame(c(list(
x = c(data$x.xmin, data$y.xmin),
xend = c(data$x.xmax, data$y.xmax),
y = c(data$x.ymin, data$y.ymin),
yend = c(data$x.ymax, data$y.ymax),
alpha = c(data$alpha, data$alpha)
), geom_aes
), n = 2)
# turn the stats data into a GeomPath
geom_grob <- GeomSegment$draw_panel(unique(geom_stats),
panel_params, coord)
# pass the GeomPath to grobTree
ggname("geom_shadows", grobTree(geom_grob))
},
# set legend box styles
draw_key = draw_key_path,
# set default aesthetics
default_aes = aes(
colour = "blue",
fill = "red",
size = 1,
linetype = 1,
alpha = 1
)
)stat-shadows.r
#' documentation ought to be here
stat_shadows <-
function(mapping = NULL,
data = NULL,
geom = "shadows",
position = "identity",
...,
# do I need to add the geom_shadows arguments here?
anchor = list(x = 0, y = 0),
shadows = list("x", "y"),
type = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
stat = StatShadows,
data = data,
mapping = mapping,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
# geom_shadows argument repeated here?
anchor = anchor,
shadows = shadows,
type = type,
na.rm = na.rm,
...
)
)
}
StatShadows <-
ggproto("StatShadows", Stat,
# do I need to repeat required_aes?
required_aes = c("x", "y"),
# set up the data, e.g. remove missing data
setup_data = function(data, params) {
data
},
# set up parameters, e.g. unpack from list
setup_params = function(data, params) {
params
},
# calculate shadows: returns data_frame with colnames: xmin, xmax, ymin, ymax
compute_group = function(data, scales, anchor = list(x = 0, y = 0), shadows = list("x", "y"), type = NULL, na.rm = TRUE) {
.compute_shadows(data = data, anchor = anchor, shadows = shadows, type = type)
}
)
# Calculate the shadows for each type / shadows / anchor
.compute_shadows <- function(data, anchor, shadows, type) {
# Deleted all type-checking, etc. for MWE
# Only 'type = c(double, double)' accepted, e.g. type = c(0, 1)
qs <- type
# compute shadows along the x-axis
if (any(shadows == "x")) {
shadows.x <- c(
xmin = as.numeric(stats::quantile(data[, "x"], qs[[1]])),
xmax = as.numeric(stats::quantile(data[, "x"], qs[[2]])),
ymin = anchor[["y"]],
ymax = anchor[["y"]])
}
# compute shadows along the y-axis
if (any(shadows == "y")) {
shadows.y <- c(
xmin = anchor[["x"]],
xmax = anchor[["x"]],
ymin = as.numeric(stats::quantile(data[, "y"], qs[[1]])),
ymax = as.numeric(stats::quantile(data[, "y"], qs[[2]])))
}
# store shadows in one data_frame
stats <- new_data_frame(c(x = shadows.x, y = shadows.y))
# return the statistics
stats
}
.发布于 2018-12-30 17:59:22
直到有一个更彻底的答案:你失踪了
extra_params = c("na.rm", "shadows", "anchor", "type"),内部GeomShadows <- ggproto("GeomShadows", Geom,
也可能在StatShadows <- ggproto("StatShadows", Stat,内部。
在geom-.r和stat-.r中,有许多非常有用的注释来说明geoms和stats是如何工作的。特别是( github杂志上的Claus Wilke ):
# Most parameters for the geom are taken automatically from draw_panel() or
# draw_groups(). However, some additional parameters may be needed
# for setup_data() or handle_na(). These can not be imputed automatically,
# so the slightly hacky "extra_params" field is used instead. By
# default it contains `na.rm`
extra_params = c("na.rm"),https://stackoverflow.com/questions/53931252
复制相似问题