I am trying to add a line segment intervals to a ggplot
. However, I am trying to make the line segments a gradient that fades the alpha to zero at each end of the interval.
For example, if I create a plot that includes some intervals, like so:
library(ggplot2)
library(dplyr)
df <- data.frame(
name = c('x1', 'x2', 'x3'),
value = c(0.251, 0.207, 0.182),
seg = c(0.027, 0.028, 0.049)
)
p <- df %>%
ggplot() + aes(x=name, y=value) +
geom_segment(aes(x=name, xend=name, y=(value - seg), yend=(value + seg),
col = name),
size = 5) +
geom_point(aes(x=name, y = value), shape = 18, size = 5, color = "black") +
theme_bw() +
xlab('Name') +
ylab("Value")
p
This produces a plot like this:
However, I am trying to figure out a way to turn those line segments into a thicker bar, that fades the alpha to zero at each end.
Something like the plot below (which can be found here)... but Im not sure how to create this style of plot with my data.
For clarity, I am just trying to recreate the coloured gradient intervals from the plot above.
EDIT
As per one of the comments, I tried using geom_link
for this... but I cant seem to get it to work correctly.
To begin, I created a vector of alpha values to pass to geom_link
like so:
alpLow <- seq(from = 0, to = 1, length.out = 150)
alpHigh <- rev(alpLow)
alp <- c(alpLow, alpHigh)
And then used this in geom_link
:
p <- df %>%
ggplot() + aes(x=name, y=value) +
geom_link(aes(x = name, y = (value - seg),
xend = name, yend = (value + seg),
colour = stat(index)), lineend = "round", size = 5, alpha = alp)
As you can see, it only applies the gradient at the start and end of the 1st and last line segment.
Furthermore, Im not entirely pleased with the aesthetic of using geom_link
as it seems to link together points (which are visible when you zoom in)... I was really hoping for a solid bar that fades the alpha.