list 从 R 中的列表绘制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32874377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:09:30  来源:igfitidea点击:

Plot data from lists in R

rlistplot

提问by Bobesh

here is the problem. I have two lists of vectors, sorted (i hope, but i thing i can sort them if they arent) where ith vector in first list has as many numbers as ith vector in the second list. I just want to plot them. But I fear R cant plot elements of lists. Any ideas how to fix that? Thx a lot. Here is the code i tried.

这是问题所在。我有两个向量列表,已排序(我希望,但如果它们不存在,我可以对它们进行排序),其中第一个列表中的第 i 个向量与第二个列表中的第 i 个向量的数字一样多。我只是想绘制它们。但我担心 R 不能绘制列表的元素。任何想法如何解决这个问题?多谢。这是我试过的代码。

a<-c(2,1,5)
b<-c(2,2,2)
f<-c(4)
g<-c(1)
k<-list(a,f)
l<-list(b,g)
for(i in 1:2){
plot(l[i],k[i])}

and the issue is

问题是

Error in xy.coords(x, y, xlabel, ylabel, log) : 
(list) object cannot be coerced to type 'double'

回答by LyzandeR

The best way to do it is to avoid the for-loopand unlist the lists in order to plot them.

最好的方法是避免for-loop和取消列出列表以绘制它们。

This is a way using unlist:

这是一种使用方法unlist

plot(unlist(l),unlist(k))

enter image description here

在此处输入图片说明

The way to do it with a for-loopwould be the following:

使用 afor-loop的方法如下:

for (i in 1:2) {
  par(new=T)
  plot(l[[i]], k[[i]], xlim=c(0,2), ylim=c(0,5))
}

But it is totally unnecessary as you can get the same result simply by unlisting. You would also have to use par(new=T)so that the second (or any other) plot won't overwrite the previous ones and you would have to specify x and y limits so that the two plots would have the same scales. Also, you would have to use double square brackets [[]]as @HubertL mentions in his answer to access the lists. The result would be the same as above (with the labels in a bolder format since labels would be plotted twice on top of each other).

但这完全没有必要,因为您只需取消列出即可获得相同的结果。您还必须使用,par(new=T)以便第二个(或任何其他)图不会覆盖以前的图,并且您必须指定 x 和 y 限制,以便两个图具有相同的比例。此外,您必须使用方括号[[]]作为@HubertL 在他的回答中提到的访问列表。结果将与上述相同(标签采用更粗的格式,因为标签将在彼此之上绘制两次)。

回答by HubertL

You can try to use double brackets[[]]:

您可以尝试使用双括号[[]]:

plot(l[[i]],k[[i]])

回答by Sergio

Almost there, as @HubertL, just two brackets

几乎就在那里,作为@HubertL,只有两个括号

a<-c(2,1,5)
b<-c(2,2,2)
f<-c(4)
g<-c(1)
k<-list(a,f)
l<-list(b,g)
for(i in 1:2){
plot(l[[i]],k[[i]])
}

Firstenter image description here

第一的在此处输入图片说明