r - How to draw transparent lines in ggplot2 -
i want hide elements in ggplot2 facets depending on given variable. first idea switch elements transparent, not disappear plot (see image below).
for label value less 1 dont want plot segment , text.
library(tidyverse) library(ggplot2) cardata <- mtcars # max each gear facet df2 <- cardata %>% group_by(gear) %>% summarise(ypos = max(mpg)*1.1) %>% mutate(x = 1, xend = 2) # use factor level locators df2$trans <- c(0,0,1) df2$label <- c(0.1,0.1,3) cardata$cyl <- factor(cardata$cyl) p <- ggplot(cardata, aes(cyl, mpg)) + geom_boxplot() + geom_segment(data = df2, aes(y = ypos, yend = ypos, x = x, xend = xend,alpha=trans)) + geom_text(data = df2, aes(y = ypos*1.02, x = mean(c(x, xend)),label=label,alpha=trans))+ facet_wrap( ~ gear,ncol=2, scales="free") p
Comments
Post a Comment