ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

geom_hline and geom_vline legend issues

Open rdcomineor opened this issue 10 years ago • 4 comments

Hello, Unfortunately a problem persists in the legends of the lines if you also have to add a horizontal line:

threshold <- data.frame(x = c(1.2, 1.5), name = c("bar", "foo")) ggplot(df, aes(x, y, colour = z)) + geom_point() + geom_vline(aes(xintercept = 1.25, linetype = "dotted"), data = threshold)+geom_hline(aes(yintercept = 1.75, linetype = "1F"), data = threshold)

Do you have a suggestion how to have just a vertical line, or horizontal line and not a cross in the legends? Many thanks

image

rdcomineor avatar Mar 23 '16 15:03 rdcomineor

Please provide a reproducible example. There are three things you need to include to make your example reproducible: required packages, data, and code.

  1. Packages should be loaded at the top of the script, so it's easy to see which ones the example needs. This is a good time to check that you're using the latest version of each package: it's possible you've discovered a bug that been fixed since you installed the package.
  2. The easiest way to include data in a question is to use dput() to generate the R code to recreate it. For example, to recreate the mtcars dataset in R, I'd perform the following steps:
  3. Run dput(mtcars) in R
  4. Copy the output
  5. In my reproducible script, type mtcars <- then paste.

Try and find the smallest subset of your data that still reveals the problem. 3. Spend a little bit of time ensuring that your code is easy for others to read:

  • Make sure you've used spaces and your variable names are concise, yet informative.
  • Use comments to indicate where your problem lies.
  • Do your best to remove everything that is not related to the problem.
    The shorter your code is, the easier it is to understand, and the easier it is to fix.

Finish by checking that you have actually made a reproducible example by starting a fresh R session and copying and pasting your script in.

hadley avatar Jul 28 '16 14:07 hadley

The 'problem' is that the two geoms share the same aesthetic and the legend is therefore combined. hline brings the horizontal key and vline brings the vertical together forming a cross. This is difficult to avoid as the key makes sense in all cases where the aesthetic is not directly related to the geom. Imagine a case where you map linetype to a value that could be present in the data for both the horizontal and vertical line - in that case you want the merged legend to indicate that the linetype can be related to either geom...

There will need to be a thorough rewrite of the legend merging if this is to be addressed and I'm not sure it is even possible - guides/legends have no clue about how their data is represented across layers...

thomasp85 avatar Aug 02 '16 09:08 thomasp85

Having looked a bit more into this I'm quite sure that this is not doable with the current division of concern between layers, scales, and guides. The scale only knows about the levels across all layers, and the guide only knows about the scale.

Voting to close.

thomasp85 avatar Aug 02 '16 10:08 thomasp85

Many thanks for the clarification and for looking into this! Yes, indeed, it should be closed if there is no a foreseeable solution.

rdcomineor avatar Aug 02 '16 12:08 rdcomineor