我正在尝试使用ggplot2创建一个日志正常y比例的性能图表.不幸的是,我不能像基本情节函数那样产生好的滴答声.
这是我的例子:
library(ggplot2)
library(scales)
# fix RNG
set.seed(seed = 1)
# simulate returns
y=rnorm(999, 0.02, 0.2)
# M$Y are the cummulative returns (like an index)
M = data.frame(X = 1:1000, Y=100)
for (i in 2:1000)
M[i, "Y"] = M[i-1, "Y"] * (1 + y[i-1])
ggplot(M, aes(x = X, y = Y)) + geom_line() + scale_y_continuous(trans = log_trans())
Run Code Online (Sandbox Code Playgroud)
产生难看的蜱:

我也尝试过:

ggplot(M, aes(x = X, y = Y)) + geom_line() +
scale_y_continuous(trans = log_trans(), breaks = pretty_breaks())
Run Code Online (Sandbox Code Playgroud)
如何获得与默认绘图函数相同的中断/刻度:
plot(M, type = "l", …Run Code Online (Sandbox Code Playgroud) 我在 Python 中搜索并找到了类似的帖子,而不是 R。我在这篇帖子Logarithmic y-axis Tick Marks in Rplot() or ggplot2()中使用了 Richie Cotton 的代码。我不想显示次要刻度的所有标签,我只想显示主要刻度,例如 1、10、100 等。请参阅下面的示例图片,这就是为什么我不想显示所有次要刻度的标签。我尝试删除“labels=breaks”
在代码中,但什么也没发生。
library(ggplot2)
dfr <- data.frame(x = 1:100, y = rlnorm(100))
p <- ggplot(dfr, aes(x, y)) +
geom_point() +
scale_x_log10(breaks = breaks, labels = breaks)
get_breaks <- function(x){
lo <- floor(log10(min(x, na.rm = TRUE)))
hi <- ceiling(log10(max(x, na.rm = TRUE)))
as.vector(10 ^ (lo:hi) %o% 1:9)
}
breaks <- get_breaks(dfr$x)
log10_breaks <- log10(breaks)
p + labs(axis.ticks = element_line(
size = ifelse(log10_breaks == floor(log10_breaks), 2, …Run Code Online (Sandbox Code Playgroud)