r - R图例在情节中的位置

标签 r plot legend

我有一个地块,其中的数据可以进入我想用于图例的区域。有没有一种方法可以让绘图自动在最高数据点上方的标题空间中放入图例以使其适合?

如果我手动输入ylim()参数以扩大大小,然后给出我想要图例的位置的确切坐标,则可以使它工作,但是我更愿意采用一种更灵活的方法来完成此操作,因为它是前端对于数据库查询,数据级别可能具有非常不同的级别。

最佳答案

编辑2017:

使用ggplot和theme(legend.position =“”):

library(ggplot2)
library(reshape2)

set.seed(121)
a=sample(1:100,5)
b=sample(1:100,5)
c=sample(1:100,5)

df = data.frame(number = 1:5,a,b,c)
df_long <- melt(df,id.vars = "number")
ggplot(data=df_long,aes(x = number,y=value, colour=variable)) +geom_line() +
theme(legend.position="bottom")

2012年原始答案:
将图例放在底部:
set.seed(121)
a=sample(1:100,5)
b=sample(1:100,5)
c=sample(1:100,5)

dev.off()

layout(rbind(1,2), heights=c(7,1))  # put legend on bottom 1/8th of the chart

plot(a,type='l',ylim=c(min(c(a,b,c)),max(c(a,b,c))))
lines(b,lty=2)
lines(c,lty=3,col='blue')

# setup for no margins on the legend
par(mar=c(0, 0, 0, 0))
# c(bottom, left, top, right)
plot.new()
legend('center','groups',c("A","B","C"), lty = c(1,2,3),
       col=c('black','black','blue'),ncol=3,bty ="n")

关于r - R图例在情节中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8929663/

相关文章:

r - ggplot2中同一轴上的不同刻度长度

python - 如何在同一张图中绘制不同频率的各种数据?

plot - 提高 Julia 中对数刻度的绘图质量

r - 在R data.frame中,如何根据当前现有变量添加新变量

r - 理解 R-Hive、Elastic MapReduce、RHIPE 和使用 R 的分布式文本挖掘

MATLAB : Plot multiple lines in a single graph with different colors and Legend them.

matlab - 修复图例在 MATLAB 中的位置

r - 如何在同一标签文本中切换纯文本字体和纯文本字体

R:对于大数据集,对级别进行编号并分配给新列

r - R中的分类日期变量