我正在尝试创建ggplot2扩展名。我想创建一个统计(或geom),使我能够以某种类似于well log image的方式绘制测井曲线。封装在ggproto对象中的计算最终会失败。 让我们看一下宠物的例子!假设我们有一些测井信息,可提供地质单元的下边界,其名称和位置指示符:
library(tidyverse)
testwell <- tibble::tribble(
~depth, ~type, ~pos,
3, "loam", 1,
7, "sand", 1,
7.5, "clay", 1,
11, "murl", 1,
12.2, "gravel", 1
)
为了将其转换为可绘制的多边形,我创建了一个ggproto对象和一个统计信息。
StatGeostack <- ggproto("StatGeostack", Stat,
required_aes = c("x", "y", "group"),
compute_group = function(data, scales, params, glvl = 0){
bound_low <- data$y
strata <- data$group
position <- data$x
# x-position doesn't really matter for now so let's just jitter it a bit
# and pretend we have a drillcore diameter of "1" so we can draw polygons
xmin <- position[1]-0.5
xmax <- position[1]+0.5
#The lower boundary of the first strata is the upper boundary of the second and so on
bound_up <- c(glvl, bound_low)
length(bound_up) <- length(bound_low)
# This tibble contains all the information alas not in the right format
stackframe <- tibble::tibble(
strata = strata,
bound_up = bound_up,
bound_low = bound_low
)
# adapt input format for ggplot2 polygons
purrr::pmap_dfr(stackframe,
function(strata, bound_up, bound_low, xmin, xmax){
tibble::tibble(y = c(rep(bound_up, times = 2),
rep(bound_low, times = 2)),
x = c(xmin, xmax, xmax, xmin),
group = rep(strata, times = 4))
},
xmin = xmin, xmax = xmax)
}
)
#creating the stat corresponding to the ggproto object
stat_geostack <- function(mapping = NULL, data = NULL, geom = "polygon",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, glvl = 0, ...){
layer(
stat = StatGeostack, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, glvl = 0, ...)
)
}
这就是我想出的。要了解为什么我对结果不满意,让我们在绘图中使用新的统计数据
ggplot(data = testwell,
aes(x = pos, y = depth*-1, group = type)) +
stat_geostack(aes(fill = type)) +
theme_bw()
结果看起来像这样。乍一看,这看起来还不错,但是,我们再次失去了一半的地层。在图例中,但未在面板中绘制。
我试图通过将“填充”更改为“颜色”来弄清楚发生了什么
ggplot(data = testwell,
aes(x = pos, y = depth*-1, group = type)) +
stat_geostack(fill = NA, aes(color = type), size = 2) +
theme_bw()
因此,显然发生的是丢失的地层单元没有消失,而是彼此隐藏在一起。这样做的原因似乎是,每个多边形的上边界都设置为零,而下边界是按预期计算的。我在ggproto的“ calculate_group”函数中使用的计算机制在全局环境中工作正常。我可以在ggplot2扩展机制之外计算多边形顶点,然后制作一个没有问题的常规ggplot2 + geom_polygon图,但这不是我想要的。
感谢您在这份精疲力尽的帖子中与我在一起。