在 R 中迭代生成名称以存储绘图
我正在使用 R 循环数据框、执行计算并绘制图表。
for(i in 2 : 15){
# get data
dataframe[,i]
# do analysis
# make plot
a <- plot()
}
有没有办法让绘图对象名称为“a”,使用“i”的值?例如,a+“i”<-plot()。然后我想将其添加到向量中,这样我就有了一系列绘图,可以在稍后阶段制作 pdf 时使用。或者也许还有另一种存储方式。
我熟悉 Paste() 函数,但我还没有弄清楚如何使用它来定义对象。
I'm using R to loop through a data frame, perform a calculation and to make a plot.
for(i in 2 : 15){
# get data
dataframe[,i]
# do analysis
# make plot
a <- plot()
}
Is there a way that I can make the plot object name 'a', using the value of 'i'? For example, a + "i" <- plot(). Then I want to add that to a vector so I have a series of plots that I can then use at a later stage when I want to make a pdf. Or perhaps there is another way of storing this.
I'm familiar with the paste() function but I haven't figured out how to define an object using it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要绘图对象的“向量”,最简单的方法可能是将它们存储在
列表
中。使用paste()
为您的绘图创建一个名称,然后将其添加到列表中:如果您不想将绘图存储在列表中,并且实际上想要创建一个具有以下内容的新对象: name 包含在
pltName
中,那么您可以使用assign()
:If you want a "vector" of plot objects, the easiest way is probably to store them in a
list
. Usepaste()
to create a name for your plot and then add it to the list:If you didn't want to store the plots in a list and literally wanted to create a new object that had the name contained in
pltName
, then you could useassign()
:看一下
lattice
或ggplot2
包,这些包中的绘图函数创建可以分配给变量并可以在稍后阶段打印或绘制的对象。例如使用
lattice
:或者将对象附加到列表中:
Have a look at the packages
lattice
orggplot2
, the plot functions in these packages create objects which can be assigned to variables and can be printed or plotted at a later stage.For instance with
lattice
:Or append the objects to a list: