我正在尝试使用ggplot2绘制我的数据图表。出于纯粹的表面原因,我想在图例周围画一条线,以更好地区分它与图表(即在图例框周围画一个黑色轮廓)。我在任何论坛上都找不到这个问题的答案,但也许你有什么建议?
library(ggplot2)
Res = matrix(ncol = 3, nrow = 500)
Res[,1] = 1:500
Res[,2] = sin((2*pi)/100*Res[,1])
Res[,3] = cos((2*pi)/100*Res[,1])
Res = as.data.frame(Res)
colnames(Res) = c("X", "Y1", "Y2")
ggplot(Res, aes(X)) +
geom_line(aes(y = Y1, colour = "1"), size = 2) +
geom_line(aes(y = Y2, colour = "2"), size = 2) +
scale_color_discrete(name = "Y's", labels = c(" sine", " cosine")) +
theme(legend.position=c(0.9, 0.7))
theme(...)
中添加legend.background = element_rect(fill = "grey", colour = "black")
可以通过在图例周围添加黑色边框来区分图例。 - parth