Vedda
Vedda

Reputation: 7435

Remove box and points in legend

How do I remove the the box, ribbon color, and points in the legend? I would just like a straight line representing each color of the color. I've tried using guides(), but it's not changing.

Sample data:

pdat1 <- structure(list(type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("10-year", 
"20-year", "30-year"), class = "factor"), effect = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), value = c(0, 
-21.89, -27.36, -33.75, -40.57, -47.32, 0, -23, -28.31, -34.96, 
-42.6, -50.81, 0, -16.9, -22.25, -28.87, -36.4, -44.52, 0, -10.24, 
-16.8, -24.74, -33.52, -42.55, 0, -10.24, -16.8, -24.74, -33.52, 
-42.55, 0, -10.24, -16.8, -24.74, -33.52, -42.55), temp = c(0, 
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5), value_max = c(2.91, 
-19.02, -24.42, -30.88, -37.63, -44.35, 2.9, -20.09, -25.36, 
-32.05, -39.67, -47.87, 2.97, -14.02, -19.27, -25.89, -33.49, 
-41.58, 2.42, -7.74, -14.34, -22.27, -31.06, -40.02, 2.45, -7.8, 
-14.36, -22.26, -31.07, -40.07, 2.46, -7.71, -14.23, -22.23, 
-31.02, -40.05), value_min = c(-2.91, -24.76, -30.3, -36.63, 
-43.5, -50.3, -2.9, -25.91, -31.27, -37.87, -45.52, -53.75, -2.97, 
-19.77, -25.24, -31.85, -39.32, -47.46, -2.42, -12.74, -19.26, 
-27.21, -35.98, -45.08, -2.45, -12.68, -19.24, -27.22, -35.96, 
-45.02, -2.46, -12.77, -19.37, -27.25, -36.02, -45.05)), class = "data.frame", row.names = c(NA, 
-36L), .Names = c("type", "effect", "value", "temp", "value_max", 
"value_min"))

Plot Code

library(ggplot2)
ggplot(pdat1) + 
  geom_ribbon(aes(ymax = value_max, ymin = value_min, x = temp, linetype = NA, color = effect, fill = effect), fill = "#C0CCD9", alpha = 0.5 ) +
  geom_line(aes(x = temp, y = value, color = effect, group = effect)) + 
  geom_point(aes(x = temp, y = value, color = effect), size = 0.5) +
  ylab("Y") +
  xlab("X") +
  guides(color = guide_legend(keywidth = 2,
                              keyheight = 1,
                              override.aes = list(linetype = c(1, 1),
                                                  size = 1,
                                                  shape = c(0, 0)))) +
  facet_wrap(~type)

enter image description here

Upvotes: 1

Views: 3037

Answers (1)

Gilles San Martin
Gilles San Martin

Reputation: 4370

Your ggplot code is a little bit messy, particularly for the ribbon. For example the fill aestetic is both mapped to the effect variable and set to a color value (#C0CCD9).
To remove the boxes in the legend key you need to use legend.key in theme but it works only after cleaning your ggplot code.
To avoid unnecessary repetitions I have moved severeal aestetics to the first ggplot call so that ggplot use them as default for the subsequent geom_XX calls.

ggplot(pdat1, aes(x = temp, y = value, group = effect)) + 
    geom_ribbon(aes(ymax = value_max, ymin = value_min), fill = "#C0CCD9", alpha = 0.5 ) +
    geom_line(aes(color = effect)) + 
    geom_point(aes(color = effect), size = 0.5) +
    ylab("Y") + xlab("X") +
    guides(color = guide_legend(keywidth = 2, keyheight = 1,
                                override.aes = list(size = 1, shape = NA))) +
    facet_wrap(~type) +
    theme_bw() +
    theme(legend.key = element_rect(fill = NA, color = NA))

Upvotes: 2

Related Questions