ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

geom_vline messes up geom_line legend, if show_guide = TRUE

Open maxheld83 opened this issue 11 years ago • 9 comments

Let's say I have some line graph via geom_line and would like to add some geom_vlines as thresholds. Let's further say that I have several such geom_vlines and would therefore require a legend.

Here's the base layer plot:

library(ggplot2)
data(mpg)

# base plot
g <- ggplot()
g <- g + geom_line(data = mpg, mapping = aes(y = cty, x = year, color = manufacturer), stat = "summary", fun.y = mean)
g

So far, so good:

baseplot


Now let's try and add the threshold via geom_hline with a custom legend:

# add threshold via geom_hline()
g + geom_vline(aes(xintercept = 2004, linetype = "Funny Law"), show_guide = TRUE) + scale_linetype_manual(values = c("Funny Law" = "dashed"))  # screws up all geom_line legends

Not so great, the base-layer legends are all screwed up:

geomhline


Ok, let's see whether we can make this work via geom_linerange() with separate dataframes:

thresholds <- data.frame(threshold = "Funny Law", year = 2004, ymin = 0, ymax = 35)
g + geom_linerange(data = thresholds, mapping = aes(x = year, ymin = ymin, ymax = ymax, linetype = threshold))  # works ok

Works ok, except that the geom_linerange doesn't fill the entire y-height of the plot, but ok:

linerange


Then wouldn't we expect the same approach to work via geom_vline() with a separate dataframe?

# add threshold via geom_hline() WITH separate data
g + geom_vline(data = thresholds, mapping = aes(xintercept = year, linetype = threshold), show_guide = TRUE)  # screws up all geom_line legends

Nope, the same problem with screwed up legends returns:

vlinesep


Bug or my mistake?


May be related:

  • http://stackoverflow.com/questions/5027016/missing-legend-with-ggplot2-and-geom-line
  • http://stackoverflow.com/questions/30807890/add-geom-hline-legend-without-screwing-up-geom-line-legend

maxheld83 avatar Aug 16 '15 10:08 maxheld83

updated, because earlier version was too complicated.

maxheld83 avatar Aug 16 '15 15:08 maxheld83

Here's an even simpler example:

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"))
ggplot(df, aes(x, y, colour = z)) + 
  geom_point() + 
  geom_vline(aes(xintercept = x, linetype = name), show.legend = TRUE, data = threshold)

hadley avatar Aug 17 '15 13:08 hadley

There are a couple of problems here. There's a bug in the legend generation code which means that the legends where inheriting aesthetics when they shouldn't. There's another problem that show.legend = TRUE forces inclusion on every legend. But that problem goes away if geom_vline() defaults to show.legend = NA.

Both these cases now work:

df <- data.frame(x = c(1, 2), y = c(1, 2), z = c("a", "b"))
ggplot(df, aes(x, y, colour = z)) + 
  geom_point() + 
  geom_vline(xintercept = 1.5))

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 = x, linetype = name), data = threshold)

hadley avatar Aug 17 '15 13:08 hadley

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 Feb 22 '16 10:02 rdcomineor

Is there any solution to the cross problem demonstrated by @rdcomineor when using both geom_hline and geom_vline?

cyberang3l avatar Dec 01 '16 19:12 cyberang3l

@cyberang3l, I couldn't run the example given by @rdcomineor, but I had a similar problem with geom_linerange() and solved it by inserting show.legend=FALSE: geom_linerange(aes(ymin = y, ymax = x, color = "vline"), show.legend=FALSE) .

The mapping to color still works and this produces a legend for the geom_linerange(), so show.legend=FALSE is not a nuclear option, but it does remove the vertical lines in the legend.

ptoche avatar Sep 30 '17 09:09 ptoche

The problem still persists if you have hline and vline in the same chart.

fmassicano avatar Mar 14 '18 22:03 fmassicano

@fmassicano, as this issue is closed, why don't you post a new issue with a working, minimal example. Also, please post screenshot with the reprex package.

https://github.com/tidyverse/reprex

ptoche avatar Mar 15 '18 01:03 ptoche

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 Sep 11 '18 01:09 lock[bot]