gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:36:24 26 4
gpt4 key购买 nike

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

如果我手动输入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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com