我的代码有什么问题? R 中的直方图,百分比为 .png
What's wrong with my code? Histograms in R with percentage as .png
Whosebug 已经帮了我很大的忙,但我很难把所有东西放在一起。
我想根据存储在 txt 文件中的数据自动生成直方图。每个文件包含一个数据列。
这是我写的原始代码并且有效:
#!/usr/bin/env Rscript
#for all .txt files in a directory
for(i in 1:length(list.files(pattern="*.txt")))
{
#reading data
histo = read.table(list.files(pattern="*.txt")[i])
#cutting off ".txt"
name = list.files(pattern="*.txt")[i]
name = strsplit(name, ".", fixed=TRUE)[[1]]
name = name[1]
png(name)
hist(
main = paste("Histogram dla probki" , name),
histo$V1,
breaks = 10,
freq = FALSE,
#looks
col = "gray",
tck = -0.015,
#labels
xlab = "srednica [um]",
ylab = "gestosc"
)
box()
dev.off()
}
这是它的样子:
http://i.stack.imgur.com/GYprl.png
我想将 y 轴更改为百分比并为每一列添加标签,如下所示:
http://www.ats.ucla.edu/stat/sas/faq/histogram_anno_hist_ods.png
我更改了我的代码,实现了我在 SO 和其他网站上找到的内容,我更正了我遇到的任何错误。当我 运行 脚本时,它没有 return 任何错误,而且它似乎有效。但是,它也不会生成任何图片。
新代码如下所示:
#!/usr/bin/env Rscript
for(i in 1:length(list.files(pattern="*.txt")))
{
#reading data
histo = read.table(list.files(pattern="*.txt")[i])
#cutting off ".txt"
name = list.files(pattern="*.txt")[i]
name = strsplit(name, ".", fixed=TRUE)[[1]]
name = name[1]
histPercent <- function(histo)
{
png(name)
h <- hist(histo$V1, plot = FALSE)
h$density <- with(h, 100*density*diff(breaks)[1])
labs <- paste(round(h$density), "%", sep="")
plot(h,
main = paste("Histogram dla próbki " , name),
histo$V1,
breaks = 10,
freq = FALSE,
#looks
col = "green",
tck = -0.015,
labels = labs,
xlab = "średnica [um]",
ylab = "udział procentowy"
)
dev.off()
}
#box()
}
怎么了?我还在函数外尝试了 png() 和 dev.off()。
您的函数 histPercent
没有正确放置,因此您没有得到任何输出。试试下面的代码...
R代码:
#!/usr/bin/env Rscript
#for all .txt files in a directory
for(i in 1:length(list.files(pattern="*.txt")))
{
#reading data
histo = read.table(list.files(pattern="*.txt")[i])
#cutting off ".txt"
name = list.files(pattern="*.txt")[i]
name = strsplit(name, ".", fixed=TRUE)[[1]]
name = name[1]
png(name)
h = hist(histo$V1)
h$density = h$counts/sum(h$counts)*100
labs <- paste(round(h$density), "%", sep="")
plot(h,main = paste("Histogram dla próbki", name), freq = F, labels = labs, histo$V1,breaks =10, col = "lightgreen", tck = -0.015, xlab = "srednica [um]", ylab = "udział procentowy")
box()
dev.off()
}
图表输出:
希望对您有所帮助。
Whosebug 已经帮了我很大的忙,但我很难把所有东西放在一起。
我想根据存储在 txt 文件中的数据自动生成直方图。每个文件包含一个数据列。
这是我写的原始代码并且有效:
#!/usr/bin/env Rscript
#for all .txt files in a directory
for(i in 1:length(list.files(pattern="*.txt")))
{
#reading data
histo = read.table(list.files(pattern="*.txt")[i])
#cutting off ".txt"
name = list.files(pattern="*.txt")[i]
name = strsplit(name, ".", fixed=TRUE)[[1]]
name = name[1]
png(name)
hist(
main = paste("Histogram dla probki" , name),
histo$V1,
breaks = 10,
freq = FALSE,
#looks
col = "gray",
tck = -0.015,
#labels
xlab = "srednica [um]",
ylab = "gestosc"
)
box()
dev.off()
}
这是它的样子: http://i.stack.imgur.com/GYprl.png
我想将 y 轴更改为百分比并为每一列添加标签,如下所示: http://www.ats.ucla.edu/stat/sas/faq/histogram_anno_hist_ods.png
我更改了我的代码,实现了我在 SO 和其他网站上找到的内容,我更正了我遇到的任何错误。当我 运行 脚本时,它没有 return 任何错误,而且它似乎有效。但是,它也不会生成任何图片。
新代码如下所示:
#!/usr/bin/env Rscript
for(i in 1:length(list.files(pattern="*.txt")))
{
#reading data
histo = read.table(list.files(pattern="*.txt")[i])
#cutting off ".txt"
name = list.files(pattern="*.txt")[i]
name = strsplit(name, ".", fixed=TRUE)[[1]]
name = name[1]
histPercent <- function(histo)
{
png(name)
h <- hist(histo$V1, plot = FALSE)
h$density <- with(h, 100*density*diff(breaks)[1])
labs <- paste(round(h$density), "%", sep="")
plot(h,
main = paste("Histogram dla próbki " , name),
histo$V1,
breaks = 10,
freq = FALSE,
#looks
col = "green",
tck = -0.015,
labels = labs,
xlab = "średnica [um]",
ylab = "udział procentowy"
)
dev.off()
}
#box()
}
怎么了?我还在函数外尝试了 png() 和 dev.off()。
您的函数 histPercent
没有正确放置,因此您没有得到任何输出。试试下面的代码...
R代码:
#!/usr/bin/env Rscript
#for all .txt files in a directory
for(i in 1:length(list.files(pattern="*.txt")))
{
#reading data
histo = read.table(list.files(pattern="*.txt")[i])
#cutting off ".txt"
name = list.files(pattern="*.txt")[i]
name = strsplit(name, ".", fixed=TRUE)[[1]]
name = name[1]
png(name)
h = hist(histo$V1)
h$density = h$counts/sum(h$counts)*100
labs <- paste(round(h$density), "%", sep="")
plot(h,main = paste("Histogram dla próbki", name), freq = F, labels = labs, histo$V1,breaks =10, col = "lightgreen", tck = -0.015, xlab = "srednica [um]", ylab = "udział procentowy")
box()
dev.off()
}
图表输出:
希望对您有所帮助。