This question already has an answer here:
这个问题已经有了答案:
- Histogram with Logarithmic Scale and custom breaks 7 answers
- 直方图以对数尺度和自定义中断7答案。
Hi I'm making histogram using R, but the number of Y axis is so large that I need to turn it into logarithmic.See below my script:
你好,我用R做直方图,但是Y轴的数目太大了,我需要把它变成对数。见下面我的脚本:
hplot<-read.table("libl")
hplot
pdf("first_end")
hist(hplot$V1, breaks=24, xlim=c(0,250000000), ylim=c(0,2000000),main="first end mapping", xlab="Coordinates")
dev.off()
So how should I change my script? thx
那么,我该如何修改我的脚本呢?谢谢
4 个解决方案
#1
18
You can save the histogram data to tweak it before plotting:
您可以保存直方图数据,在绘制之前对其进行调整:
set.seed(12345)
x = rnorm(1000)
hist.data = hist(x, plot=F)
hist.data$counts = log10(hist.data$counts)
dev.new(width=4, height=4)
hist(x)
dev.new(width=4, height=4)
plot(hist.data, ylab='log10(Frequency)')
#2
4
A histogram with the y-axis on the log scale will be a rather odd histogram. Technically it will still fit the definition, but it could look rather misleading: the peaks will be flattened relative to the rest of the distribution.
在对数尺度上的y轴的直方图将是一个相当奇怪的直方图。从技术上讲,它仍然符合这个定义,但它看起来很有误导性:相对于分布的其余部分,峰值将被压平。
Instead of using a log transformation, have you considered:
您是否考虑过使用日志转换?
-
Dividing the counts by 1 million:
将总数除以100万:
h <- hist(hplot$V1, plot=FALSE)
h < -嘘(hplot $ V1,情节= FALSE)
h$counts <- h$counts/1e6
h数< - h数/ 1美元e6
plot(h)
情节(h)
-
Plotting the histogram as a density estimate:
将直方图绘制成密度估计:
hist(hplot$V1, freq=FALSE)
嘘(hplot $ V1,频率= FALSE)
#3
1
Another option would be to use plot(density(hplot$V1), log="y")
.
另一个选项是使用plot(密度(hplot$V1), log="y")。
It's not a histogram, but it shows just about the same information, and it avoids the illogical part where a bin with zero counts is not well-defined in log-space.
它不是直方图,但它只显示了相同的信息,并且它避免了逻辑部分,在日志空间中,没有定义为零的bin。
Of course, this is only relevant when your data is continuous and not when it's really categorical or ordinal.
当然,只有当你的数据是连续的,而不是真正的分类或序数时,这才有意义。
#4
1
You can log your y-values for the plot and add a custom log y-axis afterwards.
您可以为该绘图记录y值,然后添加自定义的日志y轴。
Here is an example for a table object of random normal distribution numbers:
下面是一个关于随机正态分布的表对象的例子:
# data
count = table(round(rnorm(10000)*2))
# plot
plot(log(count) ,type="h", yaxt="n", xlab="position", ylab="log(count)")
# axis labels
yAxis = c(0,1,10,100,1000)
# draw axis labels
axis(2, at=log(yAxis),labels=yAxis, las=2)
#1
18
You can save the histogram data to tweak it before plotting:
您可以保存直方图数据,在绘制之前对其进行调整:
set.seed(12345)
x = rnorm(1000)
hist.data = hist(x, plot=F)
hist.data$counts = log10(hist.data$counts)
dev.new(width=4, height=4)
hist(x)
dev.new(width=4, height=4)
plot(hist.data, ylab='log10(Frequency)')
#2
4
A histogram with the y-axis on the log scale will be a rather odd histogram. Technically it will still fit the definition, but it could look rather misleading: the peaks will be flattened relative to the rest of the distribution.
在对数尺度上的y轴的直方图将是一个相当奇怪的直方图。从技术上讲,它仍然符合这个定义,但它看起来很有误导性:相对于分布的其余部分,峰值将被压平。
Instead of using a log transformation, have you considered:
您是否考虑过使用日志转换?
-
Dividing the counts by 1 million:
将总数除以100万:
h <- hist(hplot$V1, plot=FALSE)
h < -嘘(hplot $ V1,情节= FALSE)
h$counts <- h$counts/1e6
h数< - h数/ 1美元e6
plot(h)
情节(h)
-
Plotting the histogram as a density estimate:
将直方图绘制成密度估计:
hist(hplot$V1, freq=FALSE)
嘘(hplot $ V1,频率= FALSE)
#3
1
Another option would be to use plot(density(hplot$V1), log="y")
.
另一个选项是使用plot(密度(hplot$V1), log="y")。
It's not a histogram, but it shows just about the same information, and it avoids the illogical part where a bin with zero counts is not well-defined in log-space.
它不是直方图,但它只显示了相同的信息,并且它避免了逻辑部分,在日志空间中,没有定义为零的bin。
Of course, this is only relevant when your data is continuous and not when it's really categorical or ordinal.
当然,只有当你的数据是连续的,而不是真正的分类或序数时,这才有意义。
#4
1
You can log your y-values for the plot and add a custom log y-axis afterwards.
您可以为该绘图记录y值,然后添加自定义的日志y轴。
Here is an example for a table object of random normal distribution numbers:
下面是一个关于随机正态分布的表对象的例子:
# data
count = table(round(rnorm(10000)*2))
# plot
plot(log(count) ,type="h", yaxt="n", xlab="position", ylab="log(count)")
# axis labels
yAxis = c(0,1,10,100,1000)
# draw axis labels
axis(2, at=log(yAxis),labels=yAxis, las=2)