我想创建一个新的Geom类型:geom_ohlc(),它类似于烛台图表,用于绘制股票开盘 - 高 - 低 - 收盘数据.
在得知哈德利的这篇文章之后:我试过这个:
GeomOHLC <- ggproto(`_class` = "GeomOHLC", `_inherit` = Geom,
required_aes = c("x", "op", "hi", "lo", "cl"),
draw_panel = function(data, panel_scales, coord){
coords <- coord$transform(data, panel_scales)
browser() # <<-- here is where I found the problem
grid::gList(
grid::rectGrob(
x = coords$x,
y = pmin(coords$op, coords$cl),
vjust = 0,
width = 0.01,
height = abs(coords$op - coords$cl),
gp = grid::gpar(col = coords$color, fill = "yellow")
),
grid::segmentsGrob(
x0 = coords$x,
y0 = coords$lo,
x1 = coords$x,
y1 = coords$hi
)
)
})
geom_ohlc <- function(data = NULL, mapping = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...)
{
layer(
geom = GeomOHLC, mapping = mapping, data = data,
stat = stat, position = position, show.legend = show.legend,
inherit.aes = inherit.aes, params = list(na.rm = na.rm, ...)
)
}
dt <- data.table(x = 1:10, open = 1:10, high = 3:12, low = 0:9, close = 2:11)
p <- ggplot(dt, aes(x = x, op = open, hi = high, lo = low, cl = close)) +
geom_ohlc()
p
Run Code Online (Sandbox Code Playgroud)
为简单起见,我只是不考虑酒吧的颜色.
结果图如下:

我添加一个browser()内部ggproto功能,我发现coord$transform没有改造op,hi,lo,cl美学到interverl [0,1].如何解决这个问题?
此外,除了Hadley的文章之外,还有其他关于如何创建自己的Geom类型的文档吗?
正如OP问题下的评论中提到的,问题是aes_to_scale()内部函数transform_position(),而函数又被调用coord$transform。转换仅限于指定的变量x, xmin, xmax, xend, xintercept和 y 轴的等效项。在transform_position的帮助中提到了这一点:
描述
用于转换所有位置变量的便捷函数。
用法
trans_position(df, trans_x = NULL, trans_y = NULL, ...) 参数
trans_x, trans_y x 和 y 美学的变换函数。(将变换 x、xmin、xmax、xend 等)...传递给 trans_x 和 trans_y 的附加参数。
解决方法是使用这些变量名称而不是 OP 使用的变量名称。以下代码可用于转换变量,但在其他地方失败(请参阅末尾)。我不知道预期情节的细节,因此没有尝试修复此错误。
GeomOHLC <- ggproto(
`_class` = "GeomOHLC",
`_inherit` = Geom,
required_aes = c("x", "yintercept", "ymin", "ymax", "yend"),
draw_panel = function(data, panel_scales, coord) {
coords <- coord$transform(data, panel_scales)
#browser() # <<-- here is where I found the problem
grid::gList(
grid::rectGrob(
x = coords$x,
y = pmin(coords$yintercept, coords$yend),
vjust = 0,
width = 0.01,
height = abs(coords$op - coords$cl),
gp = grid::gpar(col = coords$color, fill = "yellow")
),
grid::segmentsGrob(
x0 = coords$x,
y0 = coords$ymin,
x1 = coords$x,
y1 = coords$ymax
)
)
}
)
geom_ohlc <-
function(data = NULL,
mapping = NULL,
stat = "identity",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...)
{
layer(
geom = GeomOHLC,
mapping = mapping,
data = data,
stat = stat,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(na.rm = na.rm, ...)
)
}
dt <-
data.table(
x = 1:10,
open = 1:10,
high = 3:12,
low = 0:9,
close = 2:11
)
p <-
ggplot(dt, aes(
x = x,
yintercept = open,
ymin = high,
ymax = low,
yend = close
)) +
geom_ohlc()
p
Run Code Online (Sandbox Code Playgroud)
这会转换变量,但会产生以下错误:
Error in unit(height, default.units) :
'x' and 'units' must have length > 0
Run Code Online (Sandbox Code Playgroud)
但希望从这里开始它可以发挥作用。
注意:我相当随意地选择了原始变量名称(op、hi、lo、cl)之间的映射。特别yintercept是看起来不太适合。也许ggplot2需要支持任意比例变量名称?