【问题标题】:Fill arrow on geom_curve ggplot2geom_curve ggplot2上的填充箭头
【发布时间】:2017-12-19 01:24:08
【问题描述】:

有没有办法让 geom_curve 上的箭头闭合?相同的代码适用于 geom_segment。也许它是一个错误?

library(tidyverse)

set.seed(123)

data <- data_frame(x = rnorm(10), y = rnorm(10))

# NO ARROWHEAD FILL

ggplot(data, aes(x, y)) + 
  geom_point() +
  geom_curve(aes(x = 0, y = 0, xend = 1, yend = 1),
             color = "black",
             arrow = arrow(type = "closed"))

# ARROWHEAD FILL WORKS

ggplot(data, aes(x, y)) + 
  geom_point() +
  geom_segment(aes(x = 0, y = 0, xend = 1, yend = 1),
             color = "black",
             arrow = arrow(type = "closed"))

【问题讨论】:

标签: r ggplot2 ggproto


【解决方案1】:

我将其称为错误,您应该提出问题。在那之前:

geom_curve2 <- function(mapping = NULL, data = NULL,
                       stat = "identity", position = "identity",
                       ...,
                       curvature = 0.5,
                       angle = 90,
                       ncp = 5,
                       arrow = NULL,
                       lineend = "butt",
                       na.rm = FALSE,
                       show.legend = NA,
                       inherit.aes = TRUE) {
  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomCurve2,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      arrow = arrow,
      curvature = curvature,
      angle = angle,
      ncp = ncp,
      lineend = lineend,
      na.rm = na.rm,
      ...
    )
  )
}

GeomCurve2 <- ggproto("GeomCurve2", GeomSegment,
  draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90,
                        ncp = 5, arrow = NULL, lineend = "butt", na.rm = FALSE) {
    if (!coord$is_linear()) {
      warning("geom_curve is not implemented for non-linear coordinates",
        call. = FALSE)
    }
    trans <- coord$transform(data, panel_params)

    curveGrob(
      trans$x, trans$y, trans$xend, trans$yend,
      default.units = "native",
      curvature = curvature, angle = angle, ncp = ncp,
      square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
      gp = gpar(
        col = alpha(trans$colour, trans$alpha),
        fill = alpha(trans$colour, trans$alpha),
        lwd = trans$size * .pt,
        lty = trans$linetype,
        lineend = lineend),
      arrow = arrow
    )
  }
)

这会导致:

ggplot(data, aes(x, y)) + 
  geom_point() +
  geom_curve2(aes(x = 0, y = 0, xend = 1, yend = 1),
             color = "black",
             arrow = arrow(type = "closed")) 

【讨论】:

  • 当,我正要粘贴完全相同的解决方案。你赢了我一分钟。甚至在我的代码中称它为geom_curve2GeomCurve2
  • 谢谢大家的帮助...我想确保在我敲响 Stack 塔之前我没有提交 PR
【解决方案2】:

要为@hrbrmstr 给出的答案添加一些有用的东西,我认为geom_segment()geom_curve() 都受到不必要的限制,因为它们不允许您将箭头填充颜色与箭头轮廓分开指定。在这里,我提供了一个geom_curve2(),允许您这样做。更改的行(相对于 ggplot2 代码)被突出显示。

# copied from ggplot2 `geom_curve`
geom_curve2 <- function(mapping = NULL, data = NULL,
                       stat = "identity", position = "identity",
                       ...,
                       curvature = 0.5,
                       angle = 90,
                       ncp = 5,
                       arrow = NULL,
                       lineend = "butt",
                       na.rm = FALSE,
                       show.legend = NA,
                       inherit.aes = TRUE) {
  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomCurve2, # call `GeomCurve2` instead of `GeomCurve`
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      arrow = arrow,
      curvature = curvature,
      angle = angle,
      ncp = ncp,
      lineend = lineend,
      na.rm = na.rm,
      ...
    )
  )
}

# copied from ggplot2 `GeomCurve`
GeomCurve2 <- ggproto("GeomCurve2", GeomSegment,
  # the following `default_aes =` statement is missing in ggplot2 `GeomCurve`
  default_aes = aes(colour = "black", fill = "black", size = 0.5, linetype = 1, alpha = NA),
  draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90,
                        ncp = 5, arrow = NULL, lineend = "butt", na.rm = FALSE) {
    if (!coord$is_linear()) {
      warning("geom_curve is not implemented for non-linear coordinates",
              call. = FALSE)
    }
    trans <- coord$transform(data, panel_params)

    curveGrob(
      trans$x, trans$y, trans$xend, trans$yend,
      default.units = "native",
      curvature = curvature, angle = angle, ncp = ncp,
      square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
      gp = gpar(
        col = alpha(trans$colour, trans$alpha),
        # the following `fill = ` statement is missing in ggplot2 `GeomCurve`
        fill = alpha(trans$fill, trans$alpha),
        lwd = trans$size * .pt,
        lty = trans$linetype,
        lineend = lineend),
      arrow = arrow
    )
  }
)

现在我们可以独立于轮廓指定箭头填充:

ggplot(data, aes(x, y)) + 
  geom_point() +
  geom_curve2(aes(x = 0, y = 0, xend = 1, yend = 1),
              color = "black", fill = "red",
              arrow = arrow(type = "closed"))

如何对geom_segment() 进行等效更改留给读者练习。

【讨论】:

  • 是的。我觉得这很奇怪,也没有fill。我会为此修改我的 PR 并抄送你
  • 天哪,我希望宏自动重新生成 ggplot2-automation 端的美学部分文档 :-)
  • K.我也将它反向移植到geom_segment,并在 PR 上抄送你。
  • @hrbrmstr 必须让文档与 CRAN 保持同步,但我同意,我们必须想出一个系统来让开发文档也以某种方式建立起来!
猜你喜欢
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2018-10-18
  • 1970-01-01
相关资源
最近更新 更多