MATLAB中loglog函数的R语言等价实现:如何绘制带原始数值刻度标签的双对数图
loglog in R Absolutely! You don’t need to manually compute log values and tweak axes—R has built-in tools that mimic MATLAB’s loglog perfectly, creating a log-scaled plot while displaying your original numeric values as tick labels.
Base R Solution
The simplest approach in base R uses the log argument in the plot() function. Setting log = "xy" enables logarithmic scaling for both axes, and automatically labels ticks with your actual data values (not their log-transformed equivalents):
# Sample data x <- c(0.5, 10, 45, 100) y <- c(2, 20, 90, 200) # Example proportional y-values # Create log-log plot with original value labels plot(x, y, log = "xy", main = "Log-Log Plot (Base R)", xlab = "X", ylab = "Y")
This produces a plot identical in behavior to MATLAB’s loglog: the axes are logarithmically scaled, but tick marks show your raw data points (like 0.5, 10, 45) instead of log₁₀ values.
ggplot2 Alternative
If you prefer using the ggplot2 package, you can achieve the same result with logarithmic scale functions that preserve original value labels:
library(ggplot2) # Create ggplot log-log plot ggplot(data.frame(x, y), aes(x, y)) + geom_point() + geom_line() + scale_x_log10() + scale_y_log10() + labs(title = "Log-Log Plot (ggplot2)", x = "X", y = "Y")
Both methods eliminate the need for the manual axis setup you were using before. The base R approach is the most direct for MATLAB users transitioning to R.
Content of the question originates from Stack Exchange, asked by Orestis

