如何覆盖 ggplot2 的轴格式?

发布于 2024-09-03 02:41:07 字数 459 浏览 1 评论 0原文

当您选择对数刻度时,ggplot2 将分隔符格式化为 10^x。我希望它不要这样做。例如,下面的代码应该显示一个带有 1、2、5 等刻度的图表,而不是 10^0、10^0.3、10^0.69 等。

library(ggplot2)
dfr <- data.frame(x = 1:100, y = rlnorm(100))
breaks <- as.vector(c(1, 2, 5) %o% 10^(-1:1))
p1 <- ggplot(dfr, aes(x, y)) + geom_point() + scale_y_log10(breaks = breaks)
print(p1)

我想向 formatter 添加一个 formatter 参数code>scale_y_log10 可以解决这个问题,但我不确定在参数中放入什么,或者选项可能记录在哪里。

When you choose a log scale, ggplot2 formats the breaks like 10^x. I'd like it to not do that. For example, the code below should display a graph with ticks at 1, 2, 5 etc, not 10^0, 10^0.3, 10^0.69 etc.

library(ggplot2)
dfr <- data.frame(x = 1:100, y = rlnorm(100))
breaks <- as.vector(c(1, 2, 5) %o% 10^(-1:1))
p1 <- ggplot(dfr, aes(x, y)) + geom_point() + scale_y_log10(breaks = breaks)
print(p1)

I guess that adding a formatter argument to scale_y_log10 would do the trick, but I'm not sure what to put in the argument, or where the options might be documented.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-09-10 02:41:07

scale_y_log10(breaks=breaks, labels=breaks 应该可以解决问题。

scale_y_log10(breaks = breaks, labels = breaks should do the trick.

流绪微梦 2024-09-10 02:41:07

从 ggplot2 版本 0.9.0 开始,此行为(将轴标签格式设置为 10^0)不再是默认行为。问题中的代码现在给出了所需的结果。

library(ggplot2)
dfr <- data.frame(x = 1:100, y = rlnorm(100))
breaks <- as.vector(c(1, 2, 5) %o% 10^(-1:1))
p1 <- ggplot(dfr, aes(x, y)) + geom_point() + scale_y_log10(breaks = breaks)
print(p1)

在此处输入图像描述

As of ggplot2 version 0.9.0, this behavior (formatting the axis labels as 10^0) is no longer the default. The code in the question now gives the desired result.

library(ggplot2)
dfr <- data.frame(x = 1:100, y = rlnorm(100))
breaks <- as.vector(c(1, 2, 5) %o% 10^(-1:1))
p1 <- ggplot(dfr, aes(x, y)) + geom_point() + scale_y_log10(breaks = breaks)
print(p1)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文