繁体   English   中英

如何在ggplot2中堆叠(重叠)图例项

[英]How to stack (overlap) legend items in ggplot2

我想知道是否可以在 ggplot2 地图(或任何允许产生相同结果的包)上“堆叠”图例的项目。

对于空间( sf等)对象,至少有一个包mapsf可以产生所需的输出,但我也想为非空间对象(数据框/小标题等)生成这种类型的图例。

在这里看到一个代表:

# A regular plot with ggplot2

library(ggplot2)

# A plot
ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(size = drat))


在此处输入图像描述

# A map with the circles of the legend "stacked" (overlapping points)


# Need to conver to sf
mtcars_sf <- sf::st_as_sf(mtcars, coords = c("wt", "mpg"))

library(mapsf)

# See the legend on the top-right corner (blue arrow)
mf_map(x = mtcars_sf)
mf_map(x = mtcars_sf, var = "drat", type = "prop",
       inches = .2)


在此处输入图像描述

正如艾伦所说,(据我所知)没有通用的方法可以做到这一点。 但是您可以为您的自定义图表将其组合在一起。 Thomas Pedersen 很棒的包 ggforce 和 patchwork 是你的朋友。

我本质上是在假装传奇。 挑战在于在主情节和图例之间获得正确的尺寸,并以合理的方式将图例缝合到主情节。 这是通过在geom_ellipse上使用精心选择的半径、相应地设置坐标比并调整两个绘图的坐标限制来实现的。 代码中有更多注释。

library(ggplot2)
library(ggforce)
library(patchwork)

## we cannot just use geom_point,
## because the size of the circles need to correspond to the legend later
## you will need to play around with this constant
r_constant <- 25
mtcars$r <- mtcars$drat / r_constant
## this is to make the points round  - it will have an effect on the panel dimension
ellipse_fac <- .1
p <-
  ggplot(mtcars) +
  geom_ellipse(aes(x0 = wt, y0 = mpg, a = r, b = r / ellipse_fac, angle = 0), fill = "darkred") +
  theme(legend.position = "none") +
  coord_equal(ellipse_fac)

## for the legend, chose rounded values from the radius range
unique_r <- unique(plyr::round_any(mtcars$r, .1))
y_circles <- floor(min(mtcars$mpg))
## I'm sorting the radii decreasingly, so that the circles overlap correctly
circles <- data.frame(x = 0, r = sort(unique_r, decreasing = TRUE))
## the segment / label poistion is also arbitrary
x_lab <- max(circles$r) + .1
y_seg <- y_circles + 2 * circles$r / ellipse_fac

p_leg <-
  ggplot(circles) +
  geom_ellipse(aes(x0 = x, y0 = y_circles + r / ellipse_fac, a = r, b = r / ellipse_fac, angle = 0), fill = "darkred", alpha = .5) +
  geom_segment(aes(x = x, xend = x_lab, y = y_seg, yend = y_seg)) +
  geom_text(aes(x = x_lab, y = y_seg, label = r), hjust = 0) +
  ## you need to set the ylimits similar to the main plot for control of legend position
  coord_equal(ratio = ellipse_fac, ylim = range(mtcars$mpg), clip = "off") +
  theme_void() +
  ## also need to set a margin
  theme(plot.margin = margin(r = .2, unit = "in"))

p + p_leg

出于美学原因,我添加了一个 alpha

暂无
暂无

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

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