gpt4 book ai didi

r - 图例未出现在 R 图中

转载 作者:行者123 更新时间:2023-12-04 13:05:56 25 4
gpt4 key购买 nike

我正在使用 R。我制作了以下图表:

#create data
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 1
c = data.frame(a,b,i)
c$col = as.factor(ifelse(c$b>10,"red", "blue"))

a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 2
d = data.frame(a,b,i)
d$col = as.factor(ifelse(d$a>10,"red", "blue"))

a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 3
e = data.frame(a,b,i)
e$col = as.factor(ifelse(e$b>10,"red", "blue"))




#create data
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 1
c = data.frame(a,b,i)
c$col = as.factor(ifelse(c$b>10,"red", "blue"))

plot_1 = plot(c$a, type = "h", col = c$col, main = "plot_1, i = 1")



plot_2 = plot(d$a, type = "h", col = d$col, main = "plot_2, i = 2")



plot_3 = plot(e$a, type = "h", col = e$col, main = "plot_3, i = 3")

enter image description here

我正在尝试为所有这些情节添加图例:

plot_1 = plot(c$a, type = "h", col = c$col, main = "plot_1, i = 1")


legend(1, 95, legend=c("Sampled", "Not Sampled"),
col=c("red", "black"), lty=1:2, cex=0.8)

plot_2 = plot(d$a, type = "h", col = d$col, main = "plot_2, i = 2")

legend(1, 95, legend=c("Sampled", "Not Sampled"),
col=c("red", "black"), lty=1:2, cex=0.8)

plot_3 = plot(e$a, type = "h", col = e$col, main = "plot_3, i = 3")

legend(1, 95, legend=c("Sampled", "Not Sampled"),
col=c("red", "black"), lty=1:2, cex=0.8)

但是这些传说都没有出现。有谁知道为什么这些传说不起作用?我知道如何在 ggplot 中执行此操作,但它不适用于 R 中的基本图。我在此处遵循此网站的说明:http://www.sthda.com/english/wiki/add-legends-to-plots-in-r-software-the-easiest-way .有人可以告诉我如何解决这个问题吗?

谢谢

最佳答案

legend位置在ylim之外即默认情况下,ylim是根据min/max计算的值,更改 ylim它出现了。即当前 ylim是在0、35,而y轴指定的位置是95(巨大的差异)

plot(c$a, type = "h", col = c$col, main = "plot_1, i = 1", 
ylim = c(0, 100))
legend(1, 95, legend=c("Sampled", "Not Sampled"),
col=c("red", "black"), lty=1:2, cex=0.8)

-输出

enter image description here

关于r - 图例未出现在 R 图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69509743/

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