R ggplot2:对数转换后的数据的自定义y轴刻度标签?

问题描述 投票:0回答:1

我见过的处理基于日志的数据转换的大多数教程都涉及使用基于日志的y轴或x轴刻度。如果我想绘制数据的基于log 10的值,并指出它们的相对实际的基于指数的实值:

library(ggplot2)
library(scales)

a <- c(5,10,15,20)
b <- c(100,210,350,750)
d <- log10(b)

test_df <- data.frame(xval = a,yval = d)
test_plt <- ggplot(data = test_df,aes(x = xval,y = yval)) +
  geom_point() + 
  scale_y_continuous(breaks = seq(1,3,by = 1),limits = c(1,3),labels = trans_format("log10", 
math_format(10^.x)))

print(test_plt)

基于代码,我得到以下结果:

Log10-based plot

显然,R试图再次将已经由log 10转换的值转换为基于log 10的值,有没有一种方法表明我希望我的y轴刻度值是10 1,10 2,10 3等(即:对绘制的值进行log 10转换,但是y标记表示log 之前的实际值10转换?还是我错误地解决了这个问题?

r ggplot2 label axis
1个回答
0
投票

您可以只将math_format()与默认参数一起使用:

test_plt <- ggplot(data = test_df,aes(x = xval,y = yval)) +
    geom_point() + 
    scale_y_continuous(breaks = seq(1,3,by = 1),
                       limits = c(1,3),
                       labels = math_format())

print(test_plt)

enter image description here

来自help("math_format")

Usage
... [Some content omitted]...

math_format(expr = 10^.x, format = force)

这是精确地所需的格式,10^.x,而不是10^.x 之后 log10转换,这是您在调用它时所得到的内部 [C0 ]

© www.soinside.com 2019 - 2024. All rights reserved.