geom_segment arrow settings - head same width as line
I think you may want to look at the gggenes package - may help you not only for the arrows :)
From the example in the vignette, I used the settings to get the head to the same width as the segment.
Another advantage of using this geom, you can use alpha
library(gggenes)
library(ggplot2)
ggplot(mydat, aes(x = start_scaff, xend = end_scaff,
y = molecule, yend = molecule)) +
geom_segment(size = 3, col = "grey80") +
geom_gene_arrow(aes(xmin = ifelse(direction == 1, start_gene, end_gene),
xmax = ifelse(direction == 1, end_gene, start_gene)),
arrowhead_height = unit(3, "mm"), arrowhead_width = unit(1, "mm")) +
geom_text(aes(x = start_gene, y = molecule, label = gene),
data = mydat, nudge_y = 0.2) +
scale_y_discrete(limits = rev(levels(mydat$molecule))) +
theme_minimal()
Defining / modifying data for draw_key
I think you first thought was correct, that it would be a required_aes
of the Geom. If we make such a Geom:
library(ggplot2)
library(rlang)
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")
GeomArrow <- ggproto(
NULL, GeomSegment,
required_aes = c("x", "y", "xend", "yend", "direction"),
draw_layer = function (self, data, params, layout, coord)
{
swap <- data$direction == "backward"
v1 <- data$x
v2 <- data$xend
data$x[swap] <- v2[swap]
data$xend[swap] <- v1[swap]
GeomSegment$draw_layer(data, params, layout, coord)
}
)
And declare an appropriate key drawing function
draw_key_arrow = function(data, params, size) {
grid::segmentsGrob(
x0 = ifelse(data$direction == "forward", 0.1, 0.9),
x1 = ifelse(data$direction == "forward", 0.9, 0.1),
# Rest is just like vanilla
y0 = 0.5, y1 = 0.5,
gp = grid::gpar(
col = alpha(data$colour %||% data$fill %||% "black", data$alpha),
fill = alpha(params$arrow.fill %||% data$colour %||% data$fill %||%
"black", data$alpha),
lwd = (data$size %||% 0.5) * .pt,
lty = data$linetype %||% 1, lineend = "butt"
),
arrow = params$arrow
)
}
Then we should be able to render the plot if (!!!) we also map the direction
aesthetic to a scale with a legend.
ggplot() +
stat_identity(
geom = GeomArrow,
data = foo,
aes(x, y, xend = xend, yend = yend, col = direction, direction = direction),
arrow = arrow(length = unit(0.3, "cm"), type = "closed"),
key_glyph = draw_key_arrow
) +
# Hijacking a generic identity scale by specifying aesthetics
scale_colour_identity(aesthetics = "direction", guide = "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
Related Topics
How to Download a Large Binary File with Rcurl *After* Server Authentication
How to Use a Character as Attribute of a Function
Adding Shade to R Lineplot Denotes Standard Error
Ggplot Dotplot: What Is the Proper Use of Geom_Dotplot
Extract Consecutive Pairs of Elements from a Vector and Place in a Matrix
What Is the "Embracing Operator" '{{ }}'
How to Draw a Contour Plot When Data Are Not on a Regular Grid
Twitter Sentiment Analysis W R Using German Language Set Sentiws
Forest Plot with Table Ggplot Coding
Ggplot Inserting Space Before Degree Symbol on Axis Label
Understanding Lm and Environment
Split a File Path into Folder Names Vector
Rstudio Shiny Not Able to Use Ggvis
Click on Points in a Leaflet Map as Input for a Plot in Shiny
R Cmd Check Latex Error: Fatal PDFlatex - Gui Framework Cannot Be Initialized
"Error: Continuous Value Supplied to Discrete Scale" in Default Data Set Example Mtcars and Ggplot2