ggplot2:图例覆盖面板边框

时间:2018-08-07 15:26:00

标签: r ggplot2 legend

我想通过ggplot2包创建一个图,该图的右上角带有theme_bw和图例。不幸的是,图例覆盖了情节的边界。考虑一下R中的以下示例:

library("ggplot2")

# Example data
df <- data.frame(x = 1:10,
                 y = 1:10,
                 col = as.factor(c(rep(1, 5), rep(2, 5))))

# Plot
ggplot(df, aes(x, y, col = col)) + 
  geom_line() +
  theme_bw() +
  theme(legend.title = element_blank(),
        legend.position = c(1, 1),
        legend.justification = c(1, 1))

问题:如何在情节周围保留黑色边框?

1 个答案:

答案 0 :(得分:1)

尝试将以下行添加到对theme的呼叫中:

    ,legend.background = element_rect(fill = NA)