简体   繁体   English

在R for循环和单个R图中创建的图形中的差异(图形)

[英]Discrepancy in graphs created in R for-loop and single R plot (graph)

I have just written my first R script. 我刚刚写了我的第一个R脚本。 I need to make numerous graphs for each item in a group and thought it would be quicker to loop through the items to create the pdfs. 我需要为组中的每个项目制作大量图表,并认为遍历这些项目以创建pdf更快。 Using almost identical code, when I use the for loop, I have the info from the y-axis placed down the middle of the plot (bad). 使用几乎相同的代码,当我使用for循环时,我将来自y轴的信息放置在绘图的中间(错误)。 If I create one pdf at a time (sans loop), no text is placed down the middle of the plot (good). 如果我一次创建一个pdf(sans循环),则图的中间不会放置任何文本(良好)。 Could someone explain why there is a difference in the graphs between these two approaches and how to get rid of the centered text that occurs in the looped pdfs. 有人可以解释为什么这两种方法的图形之间存在差异,以及如何消除循环pdf中出现的居中文本。 Thank you in advance. 先感谢您。

To create a single plot, I use the code below: 要创建单个图,请使用以下代码:

require(ggplot2)
require(reshape)
require(lattice)

header = scan('out_ordered.txt', nlines = 1, what = character())
header = header[3:length(header)]
data = read.table('out_ordered.txt', skip = 2, header = FALSE, row.names = 1)
names(data) = header
tdata = data.frame(t(data))
names(tdata)
pdf (file='Different.pdf')
plot(Bacteroidetes ~ Bacteroidetes, data = tdata, pch = 16, main = 'Bacteroidetes', xlab = 'Environment', ylab = 'Counts', axes = FALSE, las = 2)
original.parameters<-par()
par(xaxt='n')
lablist<-as.vector(c(header[1:length(header)]))
axis(1, at=1:length(header), labels = FALSE)
axis(2, at=0:63)
text(seq(1, length(header), by=1), par('usr')[3] - 0.2, labels = lablist, srt = 90, pos = 1, xpd = TRUE, offset = 2.5)
box()
dev.off()

There are some spacing issues with the labels on the x-axis and could in general use some buttons to express its flare, but it is on the right track. x轴上的标签存在一些间距问题,通常可以使用一些按钮来表示其耀斑,但它在正确的轨道上。

If I use a loop, I used this code: 如果使用循环,则使用以下代码:

#!/usr/bin/Rscript 
 # change the working directory in R to the place where you have the input file.

require(ggplot2)
require(reshape)
require(lattice)

header = scan('out_ordered.txt', nlines = 1, what = character())
header = header[3:length(header)]
data = read.table('out_ordered.txt', skip = 2, header = FALSE, row.names = 1)
names(data) = header
tdata = data.frame(t(data))
names(tdata)

for(i in names(tdata))
{
        pdf(file = paste(i, '.pdf', sep = ''))
        plot(get(i) ~ get(i), data = tdata, pch = 16, main = get(i) , xlab = 'Environment', ylab = 'Counts', axes = FALSE, las = 2)
        original.parameters<-par()
        par(xaxt='n')
        lablist<-as.vector(c(header[1:length(header)]))
        lablist
        axis(1, at=1:length(header), labels = FALSE)
        axis(2, at=0:65)
        text(seq(1, length(header), by=1), par('usr')[3] - 0.2, labels = lablist, srt = 90, pos = 1, xpd = TRUE, offset = 2.5)
        box(which = 'plot')

        dev.off()
}

Here are the images: 这是图像:

在此处输入图片说明在此处输入图片说明

The first image was one created in the looping process, the other as a standalone plot. 第一个图像是在循环过程中创建的,另一个图像是独立图。

Probably not the most elegant and it did not answer my question...I removed the for-loop, created an array in bash of the items I wanted and looped through it - each time calling the R script with the array item as the R script argument. 可能不是最优雅的,并且它没有回答我的问题...我删除了for循环,在bash中创建了想要的项的数组并循环遍历-每次调用R脚本并将数组项作为R脚本参数。 Not pretty -- but it works. 不太漂亮-但可以。

In the loop you have main = get(i) , which gets the values; 在循环中,您有main = get(i) ,它获取值; that's why those numbers are showing up in the loop version. 这就是为什么这些数字显示在循环版本中的原因。 You instead want just main=i . 相反,您只需要main=i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM