实现此目的的一种方法是编写您自己的 y 比例转换函数。 ggplot2 使用的转换函数(例如使用scale_y_log10() 时)在scales 包中定义。
简答
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打印为:
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>
在上面的答案中,我替换了:
trans <- function(x) log(x, base)
与:
trans <- function(x) log(x + 1, base)