定义 /修改draw_key的数据
这个问题提出了回答如何在ggplot2传奇中向后和向前方向显示箭头?
我认为,我认为自动定义箭头方向的好方法是传递此值,在这里,“方向” ,作为美学,并相应地交换segmentsGrob
的方向。看来draw_key _...
使用像stat和Geom一样的“数据”和“ params”,但它只是一个带有单个行的数据框架,仅包含四个变量。
## browser()
# Browse[1]> data
# colour size linetype alpha
# 1 #F8766D 0.5 1 NA
我还尝试过 - 没有成功 - 将方向添加为美学:
GeomArrow <- ggproto(NULL, GeomSegment)
GeomArrow$required_aes <- c("x", "y", "xend", "yend", "direction")
如何修改draw_key中使用的数据?
library(ggplot2)
foo <- structure(list(direction = c("backward", "forward"),
x = c(0, 0), xend = c(1, 1), y = c(1, 1.2),
yend = c(1, 1.2)), row.names = 1:2, class = "data.frame")
StatArrow <- ggproto(NULL, StatIdentity)
StatArrow$compute_layer <- function (self, data, params, layout)
{
swap <- data$direction == "backward"
v1 <- data$x
v2 <- data$xend
data$x[swap] <- v2[swap]
data$xend[swap] <- v1[swap]
data
}
# draw_key_segment_custom <- function(data, params = list(direction), size) {
## ...
## the idea was to simply switch x0 and x1 in the segmentsGrob depending on the new aesthetic
## This does not work because "data$direction" does not exist
## something like
# if(data$direction == "backward") {
# x0 = 0.9
# x1 = 0.1
# } else {
# x0 = 0.1
# x1 = 0.9
# }
## then
# grid::segmentsGrob(x0, 0.5, x1, 0.5,
##...)
## but
## browser()
# Browse[1]> data
# colour size linetype alpha
# 1 #F8766D 0.5 1 NA
# }
## direction as an aesthetic works despite this warning
ggplot() +
geom_segment(data = foo,
stat = "arrow",
aes(x, y, xend = xend, yend = yend, col = direction,
direction = direction),
arrow = arrow(length = unit(0.3, "cm"), type = "closed")
## does not work, as per above
# key_glyph = "segment_custom"
)
#> Warning: Ignoring unknown aesthetics: direction
由 reprex软件包(v2.0.1)
This question came up on answering How to show arrows in backward and forward directions in a ggplot2 legend?
I thought that a good way to automatically define the direction of the arrow in the glyph would be to pass this value, here "direction", as an aesthetic and to swap the direction of the segmentsGrob
accordingly. It seems that draw_key_...
uses "data" and "params" like Stat and Geom do, but it is only a data frame with a single row and it only contains four variables.
## browser()
# Browse[1]> data
# colour size linetype alpha
# 1 #F8766D 0.5 1 NA
I also tried - without success - to add direction as an aesthetic with:
GeomArrow <- ggproto(NULL, GeomSegment)
GeomArrow$required_aes <- c("x", "y", "xend", "yend", "direction")
How can I modify this data that is used in draw_key?
library(ggplot2)
foo <- structure(list(direction = c("backward", "forward"),
x = c(0, 0), xend = c(1, 1), y = c(1, 1.2),
yend = c(1, 1.2)), row.names = 1:2, class = "data.frame")
StatArrow <- ggproto(NULL, StatIdentity)
StatArrow$compute_layer <- function (self, data, params, layout)
{
swap <- data$direction == "backward"
v1 <- data$x
v2 <- data$xend
data$x[swap] <- v2[swap]
data$xend[swap] <- v1[swap]
data
}
# draw_key_segment_custom <- function(data, params = list(direction), size) {
## ...
## the idea was to simply switch x0 and x1 in the segmentsGrob depending on the new aesthetic
## This does not work because "data$direction" does not exist
## something like
# if(data$direction == "backward") {
# x0 = 0.9
# x1 = 0.1
# } else {
# x0 = 0.1
# x1 = 0.9
# }
## then
# grid::segmentsGrob(x0, 0.5, x1, 0.5,
##...)
## but
## browser()
# Browse[1]> data
# colour size linetype alpha
# 1 #F8766D 0.5 1 NA
# }
## direction as an aesthetic works despite this warning
ggplot() +
geom_segment(data = foo,
stat = "arrow",
aes(x, y, xend = xend, yend = yend, col = direction,
direction = direction),
arrow = arrow(length = unit(0.3, "cm"), type = "closed")
## does not work, as per above
# key_glyph = "segment_custom"
)
#> Warning: Ignoring unknown aesthetics: direction
Created on 2022-06-28 by the reprex package (v2.0.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您首先认为是正确的,那将是GEOM的
quiend_aes
。如果我们制作了这样的怪异:并声明适当的密钥绘图功能
,那么我们应该能够渲染图如果(!!!)我们还将
Direction
审美映射到带有传奇的比例。由 reprex软件包(v2.0.1)
从注释中:当前可以在
I think you first thought was correct, that it would be a
required_aes
of the Geom. If we make such a Geom:And declare an appropriate key drawing function
Then we should be able to render the plot if (!!!) we also map the
direction
aesthetic to a scale with a legend.Created on 2022-07-04 by the reprex package (v2.0.1)
From comments: The relevant source code can be currently found in ggplot2/R/guide-legend.r