ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

The cross effect in legend when use geom_hline and geom_vline in the same plot.

Open fmassicano opened this issue 8 years ago • 2 comments

That problem was very discussed in issue geom_vline messes up geom_line legend, if show_guide = TRUE, but a similar problem still happen, as was mentioned by rdcominer in this comment, when both hline and vline are in the same plot. The legend of that plot, regarding hline and vline will be overlapping.

Here's are the examples:

library(ggplot2)                                                                                                                                                                               
df <- data.frame(x = c(1, 2), y = c(1, 2), z = c("a", "b"))                                                                                                                                    
threshold <- data.frame(x = c(1.2, 1.5), name = c("bar", "foo"))                                                                                                                               

Both legends are shown (vline and hline) which procude the cross effect

ggplot(df, aes(x, y, colour = z)) +                                                                                                                                                            
geom_point() +                                                                                                                                                                                 
geom_vline(aes(xintercept = 1.25, linetype = "dotted"), colour= 'red', data = threshold) +                                                                                                     
geom_hline(aes(yintercept = 1.75, linetype = "1F"), colour= 'blue', data = threshold) +                                                                                                        
scale_linetype_manual(name = "Thresholds",values = c(2,2))                                                                                                                                     

unnamed-chunk-2-1

I tried to use show.legend = FALSE in one of them, to see what happens, but ggplot2 preserve the pattern of another one in both legends.

Then the next example, show us only geom_vline legend which preserves the vline pattern in legends

ggplot(df, aes(x, y, colour = z)) +                                                                                                                                                            
geom_point() +                                                                                                                                                                                 
geom_vline(aes(xintercept = 1.25, linetype = "dotted"), colour= 'red', data = threshold) +                                                                                                     
geom_hline(aes(yintercept = 1.75, linetype = "1F"), colour= 'blue', 
                     data = threshold, show.legend = FALSE) +                                                                                   
scale_linetype_manual(name = "Thresholds",values = c(2,2))                                                                                                                                     

unnamed-chunk-3-1

The next one, the only geom_hline legend is shown which preserve the hline pattern in legends

ggplot(df, aes(x, y, colour = z)) +                                                                                                                                                            
geom_point() +                                                                                                                                                                                 
geom_vline(aes(xintercept = 1.25, linetype = "dotted"), colour= 'red', 
                    data = threshold,show.legend = FALSE) +                                                                                 
geom_hline(aes(yintercept = 1.75, linetype = "1F"), colour= 'blue', data = threshold) +                                                                                                        
scale_linetype_manual(name = "Thresholds",values = c(2,2))                                                                                                                                     

unnamed-chunk-4-1


If is possible, would be nice to allow both hline and vline in the same plot, with a horizontal line in hline legend, and a vertical line in vline legend.

fmassicano avatar Mar 15 '18 18:03 fmassicano

Duplicate of #2492

hadley avatar Apr 27 '18 13:04 hadley

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

lock[bot] avatar Oct 24 '18 14:10 lock[bot]