【问题标题】:Minor ticks on log10 scale using ggplot2使用 ggplot2 在 log10 尺度上的小刻度
【发布时间】:2014-07-09 15:35:50
【问题描述】:

我有一个约 1.08 亿行数据的数据框,共 7 列。我使用这个 R 脚本来制作它的箱线图:

ggplot(expanded_results, aes(factor(hour), dynamic_nox)) +
  geom_boxplot(fill="#6699FF", outlier.size = 0.5, lwd=.1) +
  scale_y_log10() +
  stat_summary(fun.y=mean, geom="line", aes(group=1, colour="red")) +
  ylab(expression(Exposure~to~NO[x])) + 
  xlab(expression(Hour~of~the~day)) +
  ggtitle("Hourly exposure to NOx") +
  theme(axis.text=element_text(size=12, colour="black"),
        axis.title=element_text(size=12, colour="black"),
        plot.title=element_text(size=12, colour="black"),
        legend.position="none")

图表如下所示。这非常好,但是最好在 Y 轴的顶部有一个值。我想它应该是 1000,因为 Y 轴是 log10 比例。不过我不知道该怎么做?

有什么想法吗?

编辑:回应 DrDom: 尝试添加scale_y_log10(breaks=c(0,10,100,1000))。这样做的输出是这样的:

执行以下操作的输出: scale_y_log10(breaks=c(0,10,100,1000), limits=c(0,1000))

是一个错误:

Error in seq.default(dots[[1L]][[1L]], dots[[2L]][[1L]], length = dots[[3L]][[1L]]:
'from' cannot be NA, NaN or infinite

作为对 Jaap 的回应,他提出了以下代码:

library(ggplot2)
library(scales)

ggplot(expanded_results, aes(factor(hour), dynamic_nox)) +
  geom_boxplot(fill="#6699FF", outlier.size = 0.5, lwd=.1) +
  stat_summary(fun.y=mean, geom="line", aes(group=1, colour="red")) +
  scale_y_continuous(breaks=c(0,10,100,1000,3000), trans="log1p") +
  labs(title="Hourly exposure to NOx", x=expression(Hour~of~the~day), y=expression(Exposure~to~NO[x])) +
  theme(axis.text=element_text(size=12, colour="black"), axis.title=element_text(size=12, colour="black"),
        plot.title=element_text(size=12, colour="black"), legend.position="none")

它产生这个图表。我做错了什么吗?我仍然缺少“1000”刻度标签?考虑到大部分数据都在哪里,在 10 和 100 之间打勾也不错?

【问题讨论】:

  • 尝试添加scale_y_log10(breaks=c(0,10,100,1000))scale_y_log10(breaks=c(0,10,100,1000), limits=c(0,1000))
  • 您好 DrDom。感谢您的建议。结果已添加到我上面的帖子中。请注意,数据看起来有点不同,因为再次运行图形创建大约需要 20 分钟,所以我只是使用数据的一个子集来进行演示。还没有。 :-(
  • 我写了一个自动执行此操作的函数:stackoverflow.com/a/54325289/3082472

标签: r graph ggplot2


【解决方案1】:

您可以通过将参数 breaks= 添加到 scale_y_log10() 来修改您的对数比例,但不应有 0 值,因为从这些值中也会计算对数。

df<-data.frame(x=1:10000,y=1:10000)
ggplot(df,aes(x,y))+geom_line()+
      scale_y_log10(breaks=c(1,5,10,85,300,5000))

【讨论】:

    【解决方案2】:

    除了使用scale_y_log10,您还可以将scale_y_continuousscales 包中的日志转换一起使用。当您使用log1p 转换时,您还可以在休息时包含0scale_y_continuous(breaks=c(0,1,3,10,30,100,300,1000,3000), trans="log1p")

    您的完整代码将如下所示(注意我还在labs 中组合了标题参数):

    library(ggplot2)
    library(scales)
    
    ggplot(expanded_results, aes(factor(hour), dynamic_nox)) +
      geom_boxplot(fill="#6699FF", outlier.size = 0.5, lwd=.1) +
      stat_summary(fun.y=mean, geom="line", aes(group=1, colour="red")) +
      scale_y_continuous(breaks=c(0,1,3,10,30,100,300,1000,3000), trans="log1p") +
      labs(title="Hourly exposure to NOx", x=expression(Hour~of~the~day), y=expression(Exposure~to~NO[x])) +
      theme(axis.text=element_text(size=12, colour="black"), axis.title=element_text(size=12, colour="black"),
            plot.title=element_text(size=12, colour="black"), legend.position="none")
    

    【讨论】:

    • 我认为应该是 trans="log10" 而不是 trans="log1p" 你能澄清一下吗?
    • @TheRealJimShady 你可以同时使用。但是,当您的 dynamic_nox 的值为 0 时,将排除这些观察结果。 log1p1 添加到该值,然后计算对数值。这样,0 的值也将包括在内。见this answer for an example
    • 嗨,Jaap。再次感谢您的指导。我已将您的建议的输出放在我上面的问题中。请你看一眼好吗?我仍然缺少一些刻度线。不知道该怎么办?谢谢。
    • @TheRealJimShady 您能否在您的问题(see here for an explanation how to do that)中包含(部分)数据的dput?这将使我更容易诊断问题。
    • 我想我现在已经弄清楚了。我已经这样做了,它似乎运作良好: scale_y_continuous(breaks=c(0,10,100,300,1000,3000), trans="log1p", labels=c(0,10,100,300,1000,3000))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多