How to remove a line from the fill legend using geom_vline and geom_histogram r ggplot2

Basics: Using R statistical software, ggplot2, geom_vline and geom_histogram to visualize some data. The problem is with the legend keys.

I am trying to build a pair of histograms from some stochastic simulations, and on top of this graph a couple of lines representing the result of deterministic modeling. I have data plotted, but the legend keys for histograms have an excess black line across the middle. Can you help me remove these black lines? The following is sample code that reproduces the problem:

df1 <- data.frame(cond = factor( rep(c("A","B"), each=200) ), rating = c(rnorm(200),rnorm(200, mean=.8))) df2 <- data.frame(x=c(.5,1),cond=factor(c("A","B"))) ggplot(df1, aes(x=rating, fill=cond)) + geom_histogram(binwidth=.5, position="dodge") + geom_vline(data=df2,aes(xintercept=x,linetype=factor(cond)), show_guide=TRUE) + labs(fill='Stochastic',linetype='Deterministic') 

enter image description here Edit: image added

Cheers, Ryan

+4
source share
1 answer

One way is to reorder geom_histogram() and geom_vline() . Then add another geom_vline() without aes() by simply adding xintercept= and linetype= . This will not delete the lines, but will hide them under the headings of the color legend.

 ggplot(data=df1, aes(x=rating, fill=cond)) + geom_vline(data=df2,aes(xintercept=x,linetype=factor(cond)), show_guide=TRUE) + geom_histogram(binwidth=.5, position="dodge") + geom_vline(xintercep=df2$x,linetype=c(1,3))+ labs(fill='Stochastic',linetype='Deterministic') 

enter image description here

+3
source

All Articles