简体   繁体   English

更改频率图中的x轴

[英]Change x-axis in frequency plot

I wrote a short script to create a frequency distribution plot from raw data. 我写了一个简短的脚本,用原始数据创建频率分布图。 The only thing I cannot make right is the x-axis. 我唯一不能做对的就是x轴。 As you can see below, when the numbers are too long they got written in the e-notation which is difficult to read (also, labels are long enough to be cut out from the picture). 正如您在下面看到的那样,当数字太长时,它们以难以理解的电子记号书写(而且,标签足够长,无法从图片中切出)。

在此处输入图片说明

Normally I would use digits = X but unfortunately this notation cannot be used with the command cut . 通常我会使用digits = X但是不幸的是,这种表示法不能与命令cut一起使用。 Full code is attached. 随附完整代码。 Also, any other advice to make the graph more readable is warmly welcome. 此外,热烈欢迎任何其他建议使图表更具可读性。

##Paramaters definition
num.bins = 60 #The number of bins you want to be used
w.data = 2 #The column you have the data in

##Data loading
dataset = read.csv(file.choose())

##Calculating frequency
d.min = min(dataset[,w.data])
d.max = max(dataset[,w.data])

breaks = seq(d.min, d.max, by = (d.max-d.min)/num.bins)
d.cut = cut((dataset[,w.data]), breaks, right = FALSE, digits = 6)
d.freq = table(d.cut)

##Plot
plot(d.freq, ylab = 'Frequency', las = 2)

Weird to answer to my own question; 很奇怪地回答我自己的问题; however, I found the solution. 但是,我找到了解决方案。

The cut function has a dig.lab argument which is an equivalent of digits . cut函数具有dig.lab参数,该参数等效于digits Why two commands with the same function were implemented with different names is obscure to me. 为什么用相同的名称实现具有相同功能的两个命令对我来说还是很模糊的。

So, the amended code looks something like: 因此,修改后的代码如下所示:

##Paramaters definition
num.bins = 35 #The number of bins you want to be used
w.data = 2 #The column you have the data in

##Data loading
#dataset = read.csv(file.choose())

##Calculating frequency
d.min = min(dataset[,w.data])
d.max = max(dataset[,w.data])

breaks = seq(d.min, d.max, by = (d.max-d.min)/num.bins)
d.cut = cut((dataset[,w.data]), breaks, right = FALSE, dig.lab = 6)
d.freq = table(d.cut)

##Plot
par(mar=c(4,4.5,3,1))
par(oma=c(4,2,0,0) )
plot(d.freq, ylab = 'Frequency', las = 2)
mtext(side=3, text="Frequency Distribution", line=1.2, cex=1.5)

This results in: 结果是:

Thank you very much to @joran and @celiomsj for pointing me in the right direction. 非常感谢@joran和@celiomsj为我指出正确的方向。

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

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