R ggplot2:图例覆盖面板边框

R ggplot2:图例覆盖面板边框,r,ggplot2,legend,R,Ggplot2,Legend,我想通过ggplot2软件包创建一个带有主题和图例的绘图。不幸的是,这个传说覆盖了情节的边界。在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))

我想通过ggplot2软件包创建一个带有主题和图例的绘图。不幸的是,这个传说覆盖了情节的边界。在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))
库(“ggplot2”)
#示例数据

df尝试在调用
主题时添加以下行:

    ,legend.background = element_rect(fill = NA)

谢谢,您的代码的边框仍然是黑色的。然而,传说的背景不再是白色了。有没有办法使图例的背景保持白色,同时仍然保持绘图的边界?有点粗糙,但是如果您稍微调整图例的位置,您可以在绘图中轻推图例。对于位置(1,1),图例的角点与绘图的角点完全相同,从而提供重叠。使用(0.99,0.99),它就在边界内。@camille谢谢!我会那样做的。