【问题标题】:Remove box and points in legend删除图例中的框和点
【发布时间】:2018-01-20 22:58:12
【问题描述】:

如何删除图例中的框、色带颜色和点?我想要一条代表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)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您的ggplot 代码有点乱,尤其是功能区。例如,fill aestetic 既映射到 effect 变量,又设置为颜色值 (#C0CCD9)。
    要删除图例键中的框,您需要在 theme 中使用 legend.key,但它仅在清理您的 ggplot 代码后才有效。
    为了避免不必要的重复,我将严格的美学移到第一个 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))
    

    【讨论】:

    • 谢谢!啊,你得清理代码后才能使用。去掉图例中的点怎么样?
    • guides 中将shape = c(0, 0) 替换为shape = NA。我已经相应地更新了答案......
    • 处理类似的事情,theme(legend.key... 并没有解决我的问题,但这确实解决了来自hereguides(color=guide_legend(override.aes=list(fill=NA, colour = NA)))
    猜你喜欢
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2013-10-31
    • 2019-04-25
    相关资源
    最近更新 更多