Alpha 美学显示箭头的骨架而不是普通形状 - 如何防止它?

jak*_*kes 12 r ggplot2 geom-bar

我的目标是在条形图的末尾构建一个带有箭头的条形图。我去geom_segmentarrow定义。我想将一列映射到透明度上,但 alpha 美学似乎不适用于箭头对象。这是代码片段:

tibble(y = c(10, 20, 30), n = c(300, 100, 200), transparency = c(10, 2, 4)) %>% 
  ggplot() + geom_segment(aes(x = 0, xend = n, y = y, yend = y, alpha = transparency), 
                          colour = 'red', size = 10, arrow = arrow(length = unit(1.5, 'cm'), type = 'closed')) +
  scale_y_continuous(limits = c(5, 35))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

可以很容易地观察到arrow对象在 的值较低时看起来不太好alpha,显示其骨架而不是简单、透明的形状。有什么办法可以预防吗?

All*_*ron 17

我们可以创建一个新的 geom, geom_arrowbar,我们可以像使用任何其他 geom 一样使用它,因此在您的情况下,它只需执行以下操作即可提供所需的图:

tibble(y = c(10, 20, 30), n = c(300, 100, 200), transparency = c(10, 2, 4)) %>%
  ggplot() +
  geom_arrowbar(aes(x = n, y = y, alpha = transparency), fill = "red") +
  scale_y_continuous(limits = c(5, 35)) +
  scale_x_continuous(limits = c(0, 350))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

它包含3个参数,column_widthhead_widthhead_length,让你,如果你不喜欢默认改变箭头的形状。我们还可以根据需要指定填充颜色和其他美感:

tibble(y = c(10, 20, 30), n = c(300, 100, 200), transparency = c(10, 2, 4)) %>%
  ggplot() +
  geom_arrowbar(aes(x = n, y = y, alpha = transparency, fill = as.factor(n)),
                column_width = 1.8, head_width = 1.8, colour = "black") +
  scale_y_continuous(limits = c(5, 35)) +
  scale_x_continuous(limits = c(0, 350))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

唯一的障碍是我们必须先编写它!

按照扩展 ggplot2 小插图中的示例,我们可以geom_arrowbar以与定义其他几何体相同的方式定义我们的几何体,除了我们希望能够传入控制箭头形状的 3 个参数。这些被添加到params结果layer对象的列表中,这些对象将用于创建我们的箭头层:

library(tidyverse)

geom_arrowbar <- function(mapping = NULL, data = NULL, stat = "identity",
                          position = "identity", na.rm = FALSE, show.legend = NA,
                          inherit.aes = TRUE, head_width = 1, column_width = 1,
                          head_length = 1, ...) 
{
  layer(geom = GeomArrowBar, mapping = mapping, data = data, stat = stat,
        position = position, show.legend = show.legend, inherit.aes = inherit.aes,
        params = list(na.rm = na.rm, head_width = head_width,
                      column_width = column_width, head_length = head_length, ...))
}
Run Code Online (Sandbox Code Playgroud)

现在剩下的“所有”就是定义 aGeomArrowBar是什么。这实际上是一个ggproto类定义。其中最重要的部分是draw_panel成员函数,它获取数据帧的每一行并将其转换为箭头形状。在根据 x 和 y 坐标以及我们的各种形状参数计算出箭头的形状应该是什么之后,它会grid::polygonGrob为我们的数据的每一行生成一个并将其存储在gTree. 这形成了图层的图形组件。

GeomArrowBar <- ggproto("GeomArrowBar", Geom,
  required_aes = c("x", "y"),
  default_aes = aes(colour = NA, fill = "grey20", size = 0.5, linetype = 1, alpha = 1),
  extra_params = c("na.rm", "head_width", "column_width", "head_length"),
  draw_key = draw_key_polygon,
  draw_panel = function(data, panel_params, coord, head_width = 1,
                        column_width = 1, head_length = 1) {
    hwidth <- head_width / 5
    wid <- column_width / 10
    len <- head_length / 10
    data2 <- data
    data2$x[1] <- data2$y[1] <- 0
    zero <- coord$transform(data2, panel_params)$x[1]
    coords <- coord$transform(data, panel_params)
    make_arrow_y <- function(y, wid, hwidth) {
      c(y - wid/2, y - wid/2, y - hwidth/2, y, y + hwidth/2, y + wid/2, y + wid/2)
    }
    make_arrow_x <- function(x, len){
      if(x < zero) len <- -len
      return(c(zero, x - len, x - len , x, x - len, x - len, zero))
    }
    my_tree <- grid::gTree()
    for(i in seq(nrow(coords))){
      my_tree <- grid::addGrob(my_tree, grid::polygonGrob(
        make_arrow_x(coords$x[i], len),
        make_arrow_y(coords$y[i], wid, hwidth),
        default.units = "native",
        gp = grid::gpar(
          col = coords$colour[i],
          fill = scales::alpha(coords$fill[i], coords$alpha[i]),
          lwd = coords$size[i] * .pt,
          lty = coords$linetype[i]))) }
    my_tree}
)
Run Code Online (Sandbox Code Playgroud)

这种实现远非完美。它缺少一些重要的功能,例如合理的默认轴限制和 的能力coord_flip,如果箭头比整个列长,它会产生不美观的结果(尽管在那种情况下你可能不想使用这样的图) . 但是,如果您有负值,它会明智地将箭头指向左侧。更好的实现可能还会为空箭头添加一个选项。

简而言之,它需要进行大量调整才能消除这些(和其他)错误并使其可用于生产,但同时无需太多努力即可生成一些漂亮的图表。

reprex 包(v0.3.0)于 2020 年 3 月 8 日创建


dww*_*dww 5

你可以使用geom_gene_arrowlibrary(gggenes)

data.frame(y=c(10, 20, 30), n=c(300, 100, 200), transparency=c(10, 2, 4)) %>% 
  ggplot() + 
  geom_gene_arrow(aes(xmin = 0, xmax = n, y = y, alpha = transparency), 
                  arrowhead_height = unit(6, "mm"), fill='red') +
  scale_y_continuous(limits = c(5, 35))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 这一定是我刚刚重新发明的轮子!;) (2认同)