为r数据帧中第一列的每个值创建散点图

为r数据帧中第一列的每个值创建散点图,r,scatter-plot,equivalence,R,Scatter Plot,Equivalence,使用下面的文件,我试图创建2个散点图。当第一列等于“coat”时,一个散点图比较第二列和第三列;当第一列等于“hat”时,第二个散点图比较第二列和第三列 file.txt clothing,freq,temp coat,0.3,10 coat,0.9,0 coat,0.1,20 hat,0.5,20 hat,0.3,15 hat,0.1,5 这是我写的剧本 script.R rates = read.csv("file.txt") for(i in unique(rates[1])){

使用下面的文件,我试图创建2个散点图。当第一列等于“coat”时,一个散点图比较第二列和第三列;当第一列等于“hat”时,第二个散点图比较第二列和第三列

file.txt

clothing,freq,temp
coat,0.3,10
coat,0.9,0
coat,0.1,20
hat,0.5,20
hat,0.3,15
hat,0.1,5
这是我写的剧本

script.R

rates = read.csv("file.txt")
for(i in unique(rates[1])){
        plot(unlist(rates[2])[rates[1] == toString(i)],unlist(rates[3])[rates[1] == toString(i)])
}
我在运行时收到此错误

Error in plot.window(...) : need finite 'xlim' values
Calls: plot -> plot.default -> localWindow -> plot.window
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
Execution halted
如果我将“toString(I)”替换为“hat”,则脚本可以工作,但显然只能生成一个散点图

编辑

我稍微编辑了一下我的脚本。它为循环中的第一次迭代创建一个图,但不为第一次迭代之后的任何迭代创建一个图。 这是我的剧本

rates = read.csv("file.txt")
for(i in unique(rates[,1])){
        plot(unlist(rates[2])[rates[1] == toString(i)],unlist(rates[3])[rates[1] == toString(i)])
        file.rename("Rplots.pdf", paste(i,".pdf",sep=""))
}
这就是我执行脚本时发生的情况

name@server:/directory> ./script.R 
Warning message:
In file.rename("Rplots.pdf", paste(i, ".pdf", sep = "")) :
  cannot rename file 'Rplots.pdf' to 'hat.pdf', reason 'No such file or directory'
name@server:/directory> ls
coat.pdf  file.txt  script.R*
试试这个:

rates = read.table("file.txt",sep=',',header=TRUE)

cloth_type<-unique(rates[,1])   

for (i in 1:length(cloth_type)){

    dev.new()

    index_included=which(rates[,1]==cloth_type[i])

    plot(rates[index_included,2],rates[index_included,3],main=cloth_type[i],
    xlab="freq ", ylab="temp ", pch=19)

}   
rates=read.table(“file.txt”,sep=',,header=TRUE)

cloth_type我认为您的问题来自于对data.frame调用unique(),这将生成另一个data.frame,而不是要迭代的向量。如果全局选项将字符串作为因子导入,则应能够按如下方式并排输出绘图:

## input data
rates = data.frame(clothing = c(rep("coat", 3), rep("hat", 3)), 
                   freq = c(0.3, 0.9, 0.1, 0.5, 0.3, 0.1), 
                   temp = c(10, 0, 20, 20, 15, 5))
## store original plotting parameters
op = par(no.readonly = TRUE)
## modify plotting parameters to produce side-by-side plots
par(mfrow = c(1, 2))
## output plots
for(i in levels(rates[,1])){
  plot(rates[,2][rates[,1] == i], rates[,3][rates[,1] == i])
}
## reset plotting pars
par(op)

如果要生成单独的绘图,只需删除
par
行。

使用
ggplot

您的数据作为一个
数据框

df <- data.frame(clothing=c(rep("coat",3),rep("hat",3)),
             freq=c(0.3,0.9,0.1,0.5,0.3,0.1),
             temp=c(10,0,20,20,15,5),
             stringsAsFactors=F)

也许dplyr包会有所帮助

要安装软件包,请执行以下操作:

install.packages('dplyr')
然后,您可以使用filter函数生成单独的数据帧:

library('dplyr')

rates <- read.csv("file.txt")

cloathTypes <- unique(rates$clothing)

for(cloath in cloathTypes){
  d <- filter(rates, clothing == cloath)
  plot(d$freq, d$temp, xlab = 'Freq', ylab='Temp', main=cloath)
}
library('dplyr')
rates将
for(i in unique(rates[1]))
更改为
for(i in unique(rates[,1]))
并将
dev.new()
添加到for循环中

rates = read.csv("file.txt")
for(i in unique(rates[,1])){
        dev.new()
        plot(unlist(rates[2])[rates[1] == toString(i)],unlist(rates[3])[rates[1] == toString(i)])
        file.rename("Rplots.pdf", paste(i,".pdf",sep=""))
}

我更愿意使用一个更接近我在问题中发布的解决方案,因为我不熟悉R,并且我不了解您的很多解决方案(例如dev.new(),它,以及在plot函数的前两个逗号之前到底发生了什么)。有没有像我的代码那样的方法?在我的编辑中,我使用了
unique(rates[,1])
而不是
unique(rates[1])
,我感觉它几乎可以工作了,但我只能创建一个图形。dev.new()每次打开一个新的绘图窗口,我将其放置在循环中以绘制两个散点图,一次一个。另一种方法是使用par(…)将屏幕分成两部分,并在每个部分上绘制两部分。我正在实际程序上运行我的工作脚本,但当我创建了大约50个图形时,我得到了这个错误dev.new():使用pdf(file=“Rplots1.pdf”)错误in(function)(file=ifelse(一个文件,“Rplots.pdf”,“Rplot%03d.pdf”),:打开的设备调用过多:dev.new->do.call->执行已暂停”
rates = read.csv("file.txt")
for(i in unique(rates[,1])){
        dev.new()
        plot(unlist(rates[2])[rates[1] == toString(i)],unlist(rates[3])[rates[1] == toString(i)])
        file.rename("Rplots.pdf", paste(i,".pdf",sep=""))
}