[英]R ggplot2: custom y-axis tick labels for log-transformed data?
Most tutorials I've seen for dealing with transforming log-based data involve using a log-based y-axis or x-axis scale.我见过的大多数处理转换基于日志的数据的教程都涉及使用基于日志的 y 轴或 x 轴刻度。 If I wanted to plot the log 10 -based values of my data and indicate their relative actual exponent-based real values:如果我想 plot 记录我的数据的基于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)
Based on the code, I get the following results:根据代码,我得到以下结果:
Obviously, R is trying to convert the already log 10 -transformed values into log 10 based values again, is there a way indicate that I want my y-axis tick values to be 10 1 , 10 2 , 10 3 , etc. (ie: the the plotted values are log 10 transformed, but the y-ticks indicate the actual values before the log 10 transformation? Or am I approaching this problem incorrectly?显然, R 正在尝试将已经转换为 log 10的值再次转换为基于 log 10的值,有没有办法表明我希望我的 y 轴刻度值为 10 1 、 10 2 、 10 3等(即: 绘制的值是 log 10转换的,但是 y-ticks 表示 log 10转换之前的实际值?还是我错误地处理了这个问题?
You can just use math_format()
with its default arguments:您可以将math_format()
与默认的 arguments 一起使用:
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)
From help("math_format")
:来自help("math_format")
:
Usage
... [Some content omitted]...
math_format(expr = 10^.x, format = force)
This is exactly the formatting you want, 10^.x
, rather than 10^.x
after a log10
transformation, which is what you get when you call it within trans_format("log10", math_format(10^.x))
这正是您想要的格式, 10^.x
,而不是log10
转换后的10^.x
,这是您在trans_format("log10", math_format(10^.x))
中调用它时得到的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.