geom_curve ggplot2 上的填充箭头
Fill arrow on geom_curve ggplot2
有没有办法让 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"))
我认为这是一个错误,你应该提交一个问题。到那时:
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"))
和
为了给@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()
进行等效更改留作 reader 的练习。
有没有办法让 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"))
我认为这是一个错误,你应该提交一个问题。到那时:
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"))
和
为了给@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()
进行等效更改留作 reader 的练习。