I have some problems setting a vertical line in the legend for geom_barregardless of geom_rectand geom_point. This is quite difficult for me, because I can’t divide geom_barit geom_rectinto a group fillto set the color and alpha correctly.
Here is an example of a dataset (reproducible):
library(reshape2)
s1 <- seq(1,130000)
y1 = (cos(s1)^3)
y2 = (sin(s1)^3)
y3 = (sin(sin(s1)^3))
df1 <- data.frame(s1, y1, y2, y3)
dm1 <- melt(df1, id.var='s1')
xmin <- c(34800,36400,36700,36900,37300,39600)
xmax <- c(36300, 36500, 36700, 37000, 39000, 4000)
ymin <- c(-0.56, -0.60, -0.65, -0.56, -0.60, -0.56)
rect <- data.frame(xmin, xmax, ymin)
library(ggplot2)
ggplot() + xlim(34800, 42000) + ylim(-1, 1) + xlab("") + ylab("") +
geom_bar(data = subset(dm1, variable %in% c("y3")), aes(x=s1, y=value, fill = variable, alpha=variable, group=variable), stat = "identity", position="identity", alpha = 0.5 ) +
geom_point(data=dm1[dm1$variable!="y3", ], aes(x = s1, y = value, color = variable, alpha= variable, group=variable )) +
geom_rect(aes(NULL, NULL, xmin = rect$xmin, xmax = rect$xmax, ymin = rect$ymin, ymax = rect$ymin + 0.05, fill="rect", alpha = "rect"), alpha = 0.5) +
scale_alpha_manual(values = c(0.8, 0.5, 0.4, 0.5), name = element_blank(), guide = 'none') +
scale_fill_manual(values = c("black", "red"), name = "back", labels = c("rect", "y3")) +
scale_color_manual (values = c("green", "blue"), name="Point", labels = c("y1", "y2"))
Current output:

At this point, I want to set the red vertical line only for legend y3 (not a red square).
trying to set vline-like form for y3
+ guides(fill = guide_legend(override.aes = list(linetype = 'solid', fill=c("black", NA))))
Obviously, he cannot work because of the connection between the group fill...
Any idea or suggestion?