将ggplot geo_geom_histogram()与y-log-scale一起使用为零箱 [英] Using ggplot geo_geom_histogram() with y-log-scale with zero bins
问题描述
我有一个集合,其中有> 10000个整数,其值在1到500之间.我想以直方图的形式绘制值,但是,由于只有少数整数的值大于200,因此我想对y轴使用对数刻度.
I've got a set with >10000 integers attaining values between 1 and 500. I want to plot the values in form of a histogram, however, since only a few integers attain values greater than 200, I want to use a logarithmic scale for the y-axis.
一个箱的计数为零时出现问题,因为对数值变为-无限大.
A problem emerges, when one bin has a count of zero, since the logarithmic value goes to -infinity.
为避免这种情况,我想向每个bin添加一个伪计数1.在标准的hist()图中,我可以按照以下步骤进行操作:
To avoid this, I want to add a pseudocount of 1 to each bin. In a standard hist()-plot I can do this like follows:
hist.data = hist(data, plot=F, breaks=30)
hist.data$counts = log10(hist.data$counts + 1)
plot(hist.data, ...)
但是,我很难找到一种方法来访问ggplot中的计数.
However, I struggle to find a way to access the counts in ggplot.
是否有一种简单的方法可以解决此问题?
Is there a simple way to do this, or are there other recommended ways to deal with this problem?
推荐答案
实现此目标的一种方法是编写自己的y缩放函数.ggplot2使用的转换函数(例如,当使用 scale_y_log10()
时)在 scales
包中定义.
One way to achieve this is to write your own transformation function for the y scale. Transformations functions used by ggplot2 (when using scale_y_log10()
for instance) are defined in the scales
package.
library(ggplot2)
library(scales)
mylog10_trans <- function (base = 10)
{
trans <- function(x) log(x + 1, base)
inv <- function(x) base^x
trans_new(paste0("log-", format(base)), trans, inv, log_breaks(base = base),
domain = c(1e-100, Inf))
}
ggplot(df, aes(x=x)) +
geom_histogram() +
scale_y_continuous(trans = "mylog10")
输出
此图使用的数据:
df <- data.frame(x=sample(1:100, 10000, replace = TRUE))
df$x[sample(1:10000, 50)] <- sample(101:500, 50)
解释trans函数
让我们检查 scales :: log10_trans
;它调用 scales :: log_trans()
;现在, scales :: log_trans
打印为:
Explaining the trans function
Let's examine scales::log10_trans
; it calls scales::log_trans()
; now, scales::log_trans
prints as:
function (base = exp(1))
{
trans <- function(x) log(x, base)
inv <- function(x) base^x
trans_new(paste0("log-", format(base)), trans, inv, log_breaks(base = base),
domain = c(1e-100, Inf))
}
<environment: namespace:scales>
在上面的答案中,我替换为:
In the answer above, I replaced:
trans <- function(x) log(x, base)
具有:
trans <- function(x) log(x + 1, base)
这篇关于将ggplot geo_geom_histogram()与y-log-scale一起使用为零箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!