可以使用coord_trans()
函数将横轴或纵轴转换为对数刻度,并使用annotate()
函数在绘图区域中添加所需的线性轴。
示例代码:
library(ggplot2)
# 创建一个线性-对数刻度的散点图
df <- data.frame(x = c(1:10), y = c(1e0, 1e2, 1e4, 1e6, 1e8, 1e10, 1e12, 1e14, 1e16, 1e18))
ggplot(data = df, aes(x = x, y = y)) +
geom_point() +
scale_y_log10() +
coord_trans(x="linear", y="log10") +
annotate("text",
x = 5, y = 10^10,
label = paste0("Pearson's r = ",
round(cor(df$x, df$y, method = "pearson"), 2)))
输出的图表中,x轴为线性刻度,y轴为对数刻度,并且使用Pearson相关系数对这些数据进行了描述。