可能有点矫枉过正,但我们可以创造一个替代方案geom_path()
图例密钥大小硬编码为 0.5:
ggplot(df, aes(Depth, Value, col = Var1, shape = Var2, lty = Var2)) +
geom_path2(aes(group = interaction(Var1, Var2)), size = 0.5) +
geom_point(aes(group = interaction(Var1, Var2)), size = 1) +
scale_shape_manual(values = c(16, 5)) +
ylab("Depth [cmbsf]") +
guides(colour = guide_legend(override.aes = list(shape = 15, size = 4, linetype = 0)),
shape = guide_legend(override.aes = list(size = 4)))
所需代码geom_path2()
:
library(grid)
# Create new ggproto object that inherits from GeomPath,
# except for its draw_key function
GeomPath2 <- ggproto("GeomPath2",
GeomPath,
draw_key = function (data, params, size) {
data$linetype[is.na(data$linetype)] <- 0
segmentsGrob(0.1, 0.5, 0.9, 0.5,
gp = gpar(col = alpha(data$colour, data$alpha),
lwd = 0.5 * .pt, # originally lwd = data$size * .pt
lty = data$linetype,
lineend = "butt"),
arrow = params$arrow)
})
# define geom_path2 to be exactly the same as geom_path, except that
# it uses Geom = GeomPath2 instead of Geom = GeomPath
geom_path2 <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
..., lineend = "butt", linejoin = "round", linemitre = 10,
arrow = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) {
layer(data = data, mapping = mapping, stat = stat,
geom = GeomPath2, # originally GeomPath
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(lineend = lineend, linejoin = linejoin,
linemitre = linemitre, arrow = arrow, na.rm = na.rm,
...))
}