R ggplot:图例周围的行 [英] R ggplot: line around legend

查看:59
本文介绍了R ggplot:图例周围的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot2进行数据绘图.

I am trying to use ggplot2 for my data plotting.

出于纯粹肤浅的原因,我想在图例周围画一条线,以更好地将其与情节区分开(即,图例框周围的黑色轮廓).我在任何论坛上都找不到这个问题的答案,但是也许您有个建议?

For purely superficial reasons I would like to have a line around my legend so as to better distinguish it from the plot (i.e. a black outline around the legend box). I could not find an answer to this question on any forum, but maybe you have a tip?

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))

推荐答案

这将给出有关图例的轮廓

This will give an outline around legend

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/传奇属性

这篇关于R ggplot:图例周围的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆