简体   繁体   English

ggplot2 中单独的符号和线路键加入图例

[英]Separate symbol and line keys in ggplot2 joined legend

I have a plot similar to the following which has both l.netype and shape mappings to the same variable.我有一个类似于以下的 plot,它同时具有到同一变量的 l.netype 和 shape 映射。 I would like to keep the joined legend but would like the point in the legend to be above the line so I can better see the l.netype.我想保留连接的图例,但希望图例中的点在线上方,这样我可以更好地看到 l.netype。 Any help is appreciated.任何帮助表示赞赏。

library(tidyverse)
library(grid)

ggplot(mtcars, aes(gear, mpg, 
                   shape = factor(cyl), 
                   linetype = factor(cyl),
                   color = factor(cyl))) + 
  geom_point(size = 2) +
  stat_summary(fun = mean, geom = "line", size = 1)+
  theme(legend.key.width  = grid::unit(0.5, "inch"))

在此处输入图像描述

Thanks to @starja's cue,感谢@starja 的提示,

draw_key_point2 <- function (data, params, size) {
  `%||%` <- ggplot2:::`%||%`
  if (is.null(data$shape)) {
    data$shape <- 19
  }
  else if (is.character(data$shape)) {
    data$shape <- ggplot2:::translate_shape_string(data$shape)
  }
  stroke_size <- data$stroke %||% 0.5
  stroke_size[is.na(stroke_size)] <- 0
  grid::pointsGrob(0.5, 0.9, pch = data$shape,
                   gp = grid::gpar(col = alpha(data$colour %||% "black", data$alpha),
                                   fill = alpha(data$fill %||% "black", data$alpha),
                                   fontsize = (data$size %||% 1.5) * .pt + stroke_size * .stroke/2,
                                   lwd = stroke_size * .stroke/2))
}

ggplot(mtcars, aes(gear, mpg, 
                   shape = factor(cyl), 
                   linetype = factor(cyl),
                   color = factor(cyl))) + 
  # geom_point(size = 2) +
  geom_point(size = 2, key_glyph = draw_key_point2) +
  stat_summary(fun = mean, geom = "line", size = 1)+
  theme(legend.key.width  = grid::unit(0.5, "inch"))

带有图例点的 ggplot2 偏移了一点

Note that this function needs two unexported ggplot2 functions ( `%||%` and translate_shape_string ), which will cause this function (if added to a package) to not pass R checks.请注意,此 function 需要两个未导出ggplot2函数( `%||%`translate_shape_string ),这将导致此 function(如果添加到包中)无法通过 R 检查。 It's not hard to copy the current version of those functions into your package code, effectively freezing-in-time their bodies, recognizing that you would need to keep your package up-to-date with changes/bug-fixes in ggplot2.将这些函数的当前版本复制到您的 package 代码中并不难,有效地及时冻结了它们的主体,认识到您需要通过 ggplot2 中的更改/错误修复使您的 package 保持最新。

This function replaces one set of hard-coded defaults ( 0.5, 0.5 ) with another set of hard-coded defaults ( 0.5, 0.9 ).这个 function 将一组硬编码默认值 ( 0.5, 0.5 ) 替换为另一组硬编码默认值 ( 0.5, 0.9 )。 Obviously there are other ways to address this, such as a function returning a function:显然还有其他方法可以解决这个问题,例如 function 返回 function:

draw_key_point3 <- function(...) {
  function(data, params, size) {
    `%||%` <- ggplot2:::`%||%`
    dots <- list(...)
    colour <- dots$colour %||% dots$color %||% data$colour %||% "black"
    shape <- dots$shape %||% data$shape %||% 19L
    alpha <- dots$alpha %||% data$alpha
    if (is.character(shape)) {
      shape <- ggplot2:::translate_shape_string(shape)
    }
    stroke_size <- dots$stroke %||% data$stroke %||% 0.5
    stroke_size[is.na(stroke_size)] <- 0
    grid::pointsGrob(dots$x %||% 0.5, dots$y %||% 0.5, pch = shape,
                     gp = grid::gpar(col = alpha(colour, alpha),
                                     fill = alpha(dots$fill %||% data$fill %||% "black", alpha),
                                     fontsize = (dots$size %||% data$size %||% 1.5) * .pt + stroke_size * .stroke/2,
                                     lwd = stroke_size * .stroke/2))
  }
}
ggplot(mtcars, aes(gear, mpg, 
                   shape = factor(cyl), 
                   linetype = factor(cyl),
                   color = factor(cyl))) + 
  geom_point(size = 2, key_glyph = draw_key_point3(x=0.5, y=0.1, shape = 18)) +
  stat_summary(fun = mean, geom = "line", size = 1)+
  theme(legend.key.width  = grid::unit(0.5, "inch"))

where one can set any of the key/legend glyph components (x, y, colour, shape, alpha, fill, size).可以在其中设置任何键/图例字形组件(x、y、颜色、形状、alpha、填充、大小)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM