我正在尝试在 ggplot 中为我的线图创建自定义对数刻度,但我正在尝试制作一个温度超出图,类似于下面的图,以显示水温处于或高于特定温度的时间量温度。
set.seed(123)
n <- 500
x <- seq(0, 100, length.out = n)
y <- 16 / (1 + exp(-0.2 * (x - 50))) - 8
data <- data.frame(x = x, y = y)
G1 <- ggplot(data, aes(x = x, y = y)) +
geom_line() +
theme(axis.line = element_line(size = 1, color = "black")) +
geom_hline(yintercept = 0, lty = "dashed") +
scale_x_continuous(expand=expand_scale(mult=c(0.01, 0.01))) +
theme(legend.position = "none",
axis.title.x = element_blank(),
axis.title.y = element_blank())
温度超出图表示例:
我希望 x 轴对于值 0 到 10 是对数样式,然后我希望它是 11 到 100 的法线轴,仅显示值 0,5,10,25,50,75,100。我试过了:
G1 + scale_x_log10((breaks = c(1, 5, 10, 25, 50, 75, 100)))
但是我现在的问题是 10 - 100 的部分太紧凑,有人能够帮助解决这个问题吗?
我不确定这正是您正在寻找的,但一种方法可能是使用移位的
pseudo_log_trans()
变换,其中我们使 50 左右的区域呈线性,但对极值进行对数变换。
通常,
scales::pseudo_log_trans()
函数以 0 为中心,-1 到 1 的区域大致呈线性,超出该区域的区域进行对数变换。我将 x
移动到 x - 50
,以便转换发生在值 50 左右,并使用 . + 50
撤消标签中的这种移动。
ggplot(data, aes(x = x - 50, y = y)) +
geom_line() +
theme(axis.line = element_line(size = 1, color = "black")) +
geom_hline(yintercept = 0, lty = "dashed") +
scale_x_continuous(trans = scales::pseudo_log_trans(5),
labels = ~scales::number(. + 50),
breaks = c(1, 5, 10, 25, 50, 75, 100) - 50,
expand=expand_scale(mult=c(0.01, 0.01))) +
theme(legend.position = "none",
axis.title.x = element_blank(),
axis.title.y = element_blank())