R ggplot:围绕传奇线

时间:2018-01-22 09:05:44

标签: r plot ggplot2 legend

我正在尝试使用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))

1 个答案:

答案 0 :(得分:2)

这将概述传奇

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(colour = 'black', fill = 'white', linetype='solid'))

您可以在此处阅读有关图例设置的更多信息https://github.com/tidyverse/ggplot2/wiki/Legend-Attributes