Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - combined geom_vline and geom_smooth in legend

Tags:

r

ggplot2

I'm encountering some strange behaviour in my legend when adding a geom_smooth() and a geom_vline() in my ggplot2 chart. Here's a reproducible example:

library(ggplot2)
n <- 60

set.seed(123)
df <- data.frame(Area = sample(c("A", "B", "C", "D"), size = n, replace = TRUE),
             x = runif(n),
             y = runif(n),
             Type = sample(c("I", "II"), size = n, replace = TRUE),
             Result = sample(c("K", "L", "M"), size = n, replace = TRUE))

df.breaks <- data.frame(Area = c("B", "C"), x = c(0.8, 0.3))

ggplot(df, aes(x = x, y = y)) + 
  geom_point(aes(colour = Result, shape = Type), size = 3) + 
  geom_smooth(aes(linetype = "Smooth"), colour = "green", se = FALSE) + 
  geom_hline(yintercept = 0.3) + 
  facet_wrap(~Area) + 
  geom_vline(data = df.breaks, aes(xintercept = x, linetype = "Break"), colour = "purple") + 
  scale_colour_manual(values = c("K" = "red", "L" = "orange", "M" = "blue")) + 
  scale_linetype_manual(name = "Lines", values = c("Break" = "dashed", "Smooth" = "solid"))

enter image description here

As you will notice the "Lines" legend has both the vertical and the horizontall lines in each item, and in the first case there are a couple of dashed lines while in the second case a couple of solid lines. I'm trying to adjust my code to produce a legend with (1) a horizontal green line and a key next to it called "Smooth" and (2) a vertical purple dahsed line with a key next to it called "Break". I would appreciate some help as, no matter what I tried (including linetype inside/outside aes() etc, or using scale_linetype_identity(), or even the override.aes option in guides) I couldn't find the right combination!

I searched for similar examples and even though I found other posts with a superimposed vertical line on colour, fill, or shape etc, I coulnd't find one with a vertical line on a linetype legend such as mine. Any help will be deeply appreciated! Thanks!

like image 709
Constantinos Avatar asked Jan 14 '16 18:01

Constantinos


1 Answers

A belated reply to this question but I thought it would be better to have an answer available in case someone else (or my future self!) is interested in this.

Following @aosmith's suggestion in the comments' section and his encouragement to provide the answer, I eventually replaced the code for the plot with:

ggplot(df, aes(x = x, y = y)) + 
     geom_point(aes(colour = Result, shape = Type), size = 3) + 
     geom_hline(yintercept = 0.3) + 
     facet_wrap(~Area) + 
     geom_vline(data = df.breaks, 
                aes(xintercept = x, linetype = "Break"), colour = "purple") + 
     scale_colour_manual(values = c("K" = "red", "L" = "orange", "M" = "blue")) + 
     scale_linetype_manual(name = "Lines",
                           values = c("Break" = "dashed", "Smooth" = "solid")) + 
     geom_smooth(aes(fill = "Smooth"), method = "loess", colour = "green", se = FALSE) + 
     scale_fill_discrete(name = "")

The last two lines above replace the following line from the original code:

geom_smooth(aes(linetype = "Smooth"), colour = "green", se = FALSE) +.

The resulting chart still needs some work, esepcially with the legend spacing, but it solves the original problem.

Vertical and Horizontal Line Legend

like image 144
Constantinos Avatar answered Oct 23 '22 07:10

Constantinos