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

Creating a scatterplot for each value of the first column in an r dataframe

使用下面的文件,我正在尝试创建 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)

}   

我认为您的问题是由于在 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

轻松完成此操作

您作为 data.frame

的数据
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)

在 x 上绘制 freq,在 y 上绘制 temp,并用 clothing

着色点
ggplot(df, aes(freq, temp, colour=clothing)) +
geom_point()

也许 dplyr 包会有所帮助。

安装包:

install.packages('dplyr')

然后你可以使用过滤器函数来生成单独的数据帧:

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)
}

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=""))
}