Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fmassicano opened this issue Mar 15, 2018 · 2 comments

Comments

@fmassicano
Copy link

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.

@hadley
Copy link
Member

hadley commented Apr 27, 2018

Duplicate of #2492

@hadley hadley marked this as a duplicate of #2492 Apr 27, 2018
@hadley hadley closed this as completed Apr 27, 2018
@lock
Copy link

lock bot commented Oct 24, 2018

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 lock bot locked and limited conversation to collaborators Oct 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants