我想创建一个新的Geom类型: geom_ohlc(),它类似于Candlestick Charts,用来绘制股票的开盘-高点-低点-收盘价数据。
在学习了这个Hadley's article之后:我尝试了一下:
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为了简单起见,我不考虑条形图的颜色。
结果图如下所示:

我在ggproto函数中添加了一个browser(),我发现coord$transform没有将op,hi,lo,cl美学转换为interverl 0,1。如何解决这个问题?
此外,除了Hadley的文章之外,还有其他关于如何创建自己的Geom类型的文档吗?
发布于 2016-10-16 23:19:50
正如在OP的问题下面的注释中所提到的,问题出在transform_position()中的aes_to_scale()函数,该函数由coord$transform调用。转换仅限于名为x, xmin, xmax, xend, xintercept的变量和y轴的等效变量。Transform_position的帮助中提到了这一点:
描述
转换所有位置变量的便捷函数。
用法
transform_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这会转换变量,但会产生以下错误:
Error in unit(height, default.units) :
'x' and 'units' must have length > 0 但希望从这里开始可以让它发挥作用。
注意:我相当随意地选择了原始变量名(op,hi,lo,cl)之间的映射。特别是yintercept似乎不太适合。也许在ggplot2中需要支持任意比例的变量名?
https://stackoverflow.com/questions/40068502
复制相似问题