我想创建一个新的Geom类型:geom_ohlc(),它类似于烛台图表,用于绘制股票开盘 - 高 - 低 - 收盘数据。
了解了这个Firebase documentation on reading lists of data后:我试过了:
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
为简单起见,我只是不考虑条形的颜色。
结果图如下:
我在browser()
函数中添加了ggproto
,我发现coord$transform
没有转换op
,hi
,{{1} },lo
美学进入interverl [0,1]。如何解决这个问题?
此外,除了哈德利的文章之外,还有其他关于如何创建自己的Geom类型的文件吗?
答案 0 :(得分:2)
正如OP提出的问题中的评论中提到的,问题是aes_to_scale()
transform_position()
内部的coord$transform
函数,而x, xmin, xmax, xend, xintercept
则调用了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
的变量和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使用的变量名称。以下代码适用于转换变量,但在其他地方失败(请参见最后的内容)。我不知道预期情节的细节,所以没有尝试修复这个错误。
yintercept
这会转换变量,但会产生以下错误:
keys
但希望能从这里开始工作。
注意:我选择了原始变量名称(op,hi,lo,cl)之间的映射,而不是任意。特别zip
似乎不太合适。也许需要在ggplot2中支持任意比例变量名?