Reputation:
I am plotting some time series and the error bars (geom_errorbar
) showing the SD of the means. I am using ggplot
and I want to make the error bars visible in the legend, like this below (done in MatLab):
Is it possible?
Upvotes: 0
Views: 645
Reputation: 251
thanks to the comment from @user20650 I got this solution, that works with:
t <- data.frame(x=c("A", "B"), y=c(0.5,1), ci=c(0.25, 0.25), color=c("Yes", "No"))
library(ggplot2)
library(grid)
GeomErrorbar$draw_key <- function (data, params, size) {
data$linetype[is.na(data$linetype)] <- 0 ;
segmentsGrob(c(0.2, 0.2, 0.5),
c(0.2, 0.8, 0.2),
c(0.8, 0.8, 0.5),
c(0.2, 0.8, 0.8),
gp = gpar(col = alpha(data$colour,
data$alpha),
lwd = data$linewidth * .pt,
lty = data$linetype,
lineend = "butt"),
arrow = params$arrow) }
t %>% ggplot(aes(x=x, y=y, color=color))+
geom_point()+
geom_errorbar(aes(ymin=y-ci, ymax=y+ci))
Upvotes: 0