这是与从此答案修改的自定义几何相关的问题。给定的 geom 分组失败,所以我包含coord_munch
在 中draw_panel
,深受GeomLine
和的启发GeomPath
。它实际上在很多情况下都有效,但我觉得它经常同样失败。
特别是,它似乎在两人一组时失败(见下面的例子),并且在使用拼凑而成的某些地块上奇怪地失败了。我打开了一个issue,但还没有得到回复,对此我并不感到惊讶,我同意并认为这实际上是一个写得不好的geom的问题,而不是拼凑的问题。
我相信用于 GeomPath 的分组(在代码## Work out grouping variables for grobs
中用 标记)对于这个 grob 失败,但我不知道如何检查在两者之间创建的 munch 对象。
我的主要问题是,我如何检查这个对象?
如果有人看到并理解我的 geom 问题,我会更加感激。干杯
例子:
library(tidyverse)
## this is not an arrange problem, as shown by the correct plot using geom_path
testdf <- testdf %>% arrange(id, group, x)
与 geom_path 一起使用
ggplot(testdf, aes(x, y)) +
geom_path(aes(group = id))
geom_trail 失败
ggplot(testdf, aes(x, y)) +
geom_trail(aes(group = id))
使用颜色时更糟
ggplot(testdf, aes(x, y)) +
geom_trail(aes(group = id, color = group))
由reprex 包(v0.3.0)于 2020-07-02 创建
几何轨迹
geom_trail <-
function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...) {
layer(data = data, mapping = mapping, stat = stat, geom = GeomTrail,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, ...))
}
GeomTrail <- ggplot2::ggproto(
"GeomTrail", ggplot2::GeomPoint,
default_aes = ggplot2::aes(
shape = 19, colour = "black", size = 1.5, fill = NA, alpha = NA, stroke = 0.5,
linesize = 0.5, linetype = 1, gap = .9,
),
## tjebo:
## here is a function handle_na(), which does have no effect on the problem
draw_panel = function(data, panel_params, coord, arrow = NULL,
lineend = "butt", linejoin = "round", linemitre = 10,
na.rm = FALSE) {
if (!anyDuplicated(data$group)) {
message_wrap("geom_path: Each group consists of only one observation. ",
"Do you need to adjust the group aesthetic?")
}
# ggplot:
##must be sorted on group
data <- data[order(data$group), , drop = FALSE]
munched <- coord_munch(coord, data, panel_params)
# ggplot:
##Default geom point behaviour
if (is.character(data$shape)) {
data$shape <- translate_shape_string(data$shape)
}
coords <- coord$transform(data, panel_params)
if (unique(coords$size == 0)) {
my_points <- NULL
} else {
my_points <- grid::pointsGrob(
coords$x,
coords$y,
pch = coords$shape,
gp = grid::gpar(
col = alpha(coords$colour, coords$alpha),
fill = alpha(coords$fill, coords$alpha),
fontsize = coords$size * .pt + coords$stroke * .stroke / 2,
lwd = coords$stroke * .stroke / 2
)
)
}
# ggplot:
##Silently drop lines with less than two points, preserving order
rows <- stats::ave(seq_len(nrow(munched)), munched$group, FUN = length)
munched <- munched[rows >= 2, ]
if (nrow(munched) < 2) {
return(zeroGrob())
}
## tjebo:
## here, ggplot2:::dapply() checks which grob to use (segment or lines),
## but it also does not seem to have an effect, or at least I don't know
## to change the grob in this case
# teunbrand:
# New behaviour
## Convert x and y to units
x <- unit(munched$x, "npc")
y <- unit(munched$y, "npc")
## Work out grouping variables for grobs
n <- nrow(munched)
group_diff <- munched$group[-1] != munched$group[-n]
start <- c(TRUE, group_diff)
end <- c(group_diff, TRUE)
## teunbrand: Custom grob class
my_path <- grid::grob(
x = x, y = y,
mult = munched$gap * .pt,
name = "trail",
gp = grid::gpar(
col = alpha(munched$colour, munched$alpha)[!end], # this could also be [start]
fill = alpha(munched$colour, munched$alpha)[!end],
lwd = munched$linesize * .pt,
lty = munched$linetype,
lineend = "butt",
linejoin = "round",
linemitre = 10
),
vp = NULL,
cl = "trail"
)
ggplot2:::ggname(
"geom_trail",
grid::grobTree(my_path, my_points)
)
}
)
# not modified hook
makeContent.trail <- function(x){
# Convert npcs to absolute units
x_new <- grid::convertX(x$x, "mm", TRUE)
y_new <- grid::convertY(x$y, "mm", TRUE)
# Do trigonometry stuff
hyp <- sqrt(diff(x_new)^2 + diff(y_new)^2)
sin_plot <- diff(y_new) / hyp
cos_plot <- diff(x_new) / hyp
diff_x0_seg <- head(x$mult, -1) * cos_plot
diff_x1_seg <- (hyp - head(x$mult, -1)) * cos_plot
diff_y0_seg <- head(x$mult, -1) * sin_plot
diff_y1_seg <- (hyp - head(x$mult, -1)) * sin_plot
x0 = head(x_new, -1) + diff_x0_seg
x1 = head(x_new, -1) + diff_x1_seg
y0 = head(y_new, -1) + diff_y0_seg
y1 = head(y_new, -1) + diff_y1_seg
keep <- unclass(x0) < unclass(x1)
# Remove old xy coordinates
x$x <- NULL
x$y <- NULL
# Supply new xy coordinates
x$x0 <- unit(x0, "mm")[keep]
x$x1 <- unit(x1, "mm")[keep]
x$y0 <- unit(y0, "mm")[keep]
x$y1 <- unit(y1, "mm")[keep]
# Set to segments class
class(x)[1] <- 'segments'
x
}
数据
testdf <- tibble(
id = c("A", "B", "B", "C", "D", "A", "E", "E", "F", "F", "G", "H", "I", "J", "I", "J", "K", "L", "M", "N", "M", "O", "P", "Q", "R", "R", "S", "T", "S", "T"),
group = c("a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d", "d", "e", "e", "e", "e", "e", "e"),
x = c(41, 43, 45, 45, 45, 46, 41, 46, 53, 54, 54, 56, 35, 35, 37, 37, 44, 44, 43, 44, 45, 45, 46, 46, 44, 48, 50, 52, 53, 54),
y = structure(c(2.2, 1.8, 1.8, 2.3, 2.2, 2.2, 5.3, 2.3, 4.6, 4.6, 4.8, 4.8, 3.9, 4.1, 3.9, 4.1, 3.6, 3.7, 2.8, 2.6, 2.8, 3.1, 3.1, 2.9, 0.7, 0.7, 1, 0.8, 1, 0.8), .Names = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""))
)