【问题标题】:Log10 Y-Axis starting from 0Log10 Y 轴从 0 开始
【发布时间】:2021-12-30 19:39:55
【问题描述】:

我创建了一个条形图来显示不同地点和层的积水差异。因为一个值远高于其他值,所以我想将 y 轴设置为 log10 刻度。这一切都有效,但结果看起来相当不直观。是否可以将 y 轴的限制设置为 0,以使值为 0.2 的条不向下?

这是我使用的代码:

p2 <- ggplot(data_summary2, aes(x= Site, y= small_mean, fill= Depth, Color= Depth))+
  geom_bar(stat = "identity", position = "dodge", alpha=1)+
  geom_errorbar(aes(ymin= small_mean - sd, ymax= small_mean + sd),
                position = position_dodge(0.9),width=0.25,   alpha= 0.6)+
  scale_fill_brewer(palette = "Greens")+
  geom_text(aes(label=small_mean),position=position_dodge(width=0.9), vjust=-0.25, hjust= -0.1, size= 3)+
  #geom_text(aes(label= Tukey), position= position_dodge(0.9), size=3, vjust=-0.8, hjust= -0.5, color= "gray25")+
  theme_bw()+
  theme(legend.position = c(0.2, 0.9),legend.direction = "horizontal")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  labs(fill="Depth [cm]")+
  theme(axis.text.x = element_text(angle = 25, size =9, vjust = 1, hjust=1))+
  scale_x_discrete(labels= c( "Site 1\n(Hibiscus tillaceus)","Site 2 \n(Ceiba pentandra)","Site 3 \n(Clitoria fairchildiana)","Site 4 \n(Pachira aquatica)"))+
  #theme(legend.position = c(0.85, 0.7))+
  labs(x= "Sites\n(Type of Tree)", y= "µg Deep-Water/ g rhizosphere soil", title = "Average microbial Deep-water incorporation per Site", subtitle = "Changes over Time and Depth")+
  facet_grid(.~Time, scale = "free")
p2 + scale_y_continuous(trans = "log10")

剧情是这样的:

【问题讨论】:

  • 欢迎来到 SO,Umberto_Leckerbaer!请让这个问题可重现。这包括您尝试过的示例代码(包括列出非基础 R 包以及收到的任何错误/警告)、示例明确数据(例如,data.frame(x=...,y=...) 或来自dput(head(x)) 的输出),和给定输入的预期输出。参考:stackoverflow.com/q/5963269minimal reproducible examplestackoverflow.com/tags/r/info
  • 试试:scale_y_continuous(trans = "log", breaks = c(0, 1, 10, 100), limits = c(NA, 100))
  • 对数刻度中小于 1 的分数是什么?这些负值,所以我发现这实际上比试图将它们显示为正值更直观。此外,您显示配对数据(第 3 天和第 10 天的测量值),也许可以考虑使用散点图。将更好地显示变化。很想展示如何,但如果没有一些虚假数据是不可能的。
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: r ggplot2


【解决方案1】:

在对数刻度上没有0,因此条形图的唯一合理起点是y = 10^01

但是,您可以使用 scales::pseudo_log_trans 创建一个伪对数刻度,以将 0 包含在轴上,以便所有条形图都具有相同的方向。我借用this 的答案。 注意0 添加到breaks 以明确这是一个伪对数刻度很重要。比较下面的两个图:

library(tidyverse)
library(scales)

# make up data with distribution positive values above and below 1
d <- tibble(grp = LETTERS[1:5],
            val = 10^(-2:2))

# normal plot with true log scale doesn't contain 0
d %>%
  ggplot(aes(x = grp, y = val, fill = grp)) +
  geom_col() +
  ggtitle("On True Log Scale Bars Start at y = 1") +
  scale_y_log10() # or if you prefer: scale_y_continuous(trans = "log10")

# set range of 'linear' portion of pseudolog scale
sigma <- min(d$val)
  
# plot on pseudolog to get all bars to extend to 0
d %>%
  ggplot(aes(x = grp, y = val, fill = grp)) +
  geom_col() +
  ggtitle("On Pseudolog Scale Bars Start at y = 0") +
  scale_y_continuous(
    trans = pseudo_log_trans(base = 10, sigma = sigma),
    breaks = c(0, 10^(-2:2)),
    labels = label_number(accuracy = 0.01)
  )

reprex package (v2.0.1) 于 2021 年 12 月 30 日创建

【讨论】:

  • 谢谢,这很有帮助。我只需要删除 sigma=sigma。 R不知何故抱怨它。非常感谢
  • 很高兴它有帮助。如果这解决了您的问题,请accept the answer
猜你喜欢
  • 1970-01-01
  • 2022-01-28
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多