删除图例中的框和点

时间:2018-01-20 22:58:13

标签: r ggplot2

如何删除图例中的框,色带和点?我想要一条代表color每种颜色的直线。我尝试使用guides(),但它没有改变。

示例数据

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"))

绘图代码

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

1 个答案:

答案 0 :(得分:1)

您的ggplot代码有点乱,特别是对于功能区而言。例如,fill aestetic都映射到effect变量并设置为颜色值(#C0CCD9)。
要删除图例键中的框,您需要在legend.key中使用theme,但仅在清除ggplot代码后才能使用。 为避免不必要的重复,我已将严重的aestetics移至第一个ggplot调用,以便ggplot将其用作后续geom_XX调用的默认值。

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))