【问题标题】:removing unused part of a log-scaled y-axis in ggplot删除 ggplot 中对数缩​​放的 y 轴的未使用部分
【发布时间】:2021-05-15 04:50:10
【问题描述】:

我是 R 和 ggplot 的新手,几个小时以来一直在尝试删除 y 轴范围的一部分,但仍然没有成功。我编写了以下代码 sn-p 以使事情可重现,以防有人知道答案。所以这里是一段代码:

library(ggplot2)
p<- ggplot(data.frame(), aes(color=c("I","Y","I","Y","I","Y"), y=c(75.33,72.95,86.46,79.63,1136.09,993.27), x=c(1,1,2, 2,3, 3))) + 
  geom_line() + 
  geom_point()+
  scale_x_continuous(breaks=c(1,2,3),labels = c("A","B","C"))+
  scale_y_log10() +
  annotation_logticks(sides = "l")+
  theme(legend.position = "none")+ labs(x = "", y = "")
print(p)

情节看起来像

我想删除 100 到 900 之间的 y 轴范围,以帮助查看低于 100 的值。我已经尝试过 thisthis 但它们都不起作用(我得到了奇怪的 NA 错误, ETC)。不胜感激。

【问题讨论】:

  • 一个情节的重点是点之间的距离是可解释的。如果您删除了轴的一部分,则违反了该基本规则。连接线将毫无意义。你确定这就是你想要的吗?
  • 是的,除非有办法显示那些在 0-100 之间压缩的值的丢失信息。更清楚一点,这个图的目的不是比较 A、B、C。它是比较设置 A、设置 B、设置 C 上的蓝线和红线。

标签: r ggplot2


【解决方案1】:

如果您希望在每个设置中强调蓝色和红色,作为替代方案如何?

ggplot(df1, aes(x, y, color = color)) +
  geom_errorbar(aes(ymin = y, ymax = y)) +
  geom_point() +
  facet_wrap(~x, scales = "free") +
  expand_limits(y = 0) +
  theme(legend.position = "none")+ 
  labs(x = NULL, y = NULL)
  

输入数据

df1 <- data.frame(
  color=c("I","Y","I","Y","I","Y"),
  y = c(75.33,72.95,86.46,79.63,1136.09,993.27),
  x= c(rep(LETTERS[1:3], each = 2)))

【讨论】:

    【解决方案2】:

    您好,我不喜欢使用用户定义的转换进行“挤压”。对数刻度已经给一些分析师/读者带来了足够的痛苦。
    我支持@Jon 使用构面和scale_free 的方法。但是,您指出的其中一篇文章提供了一个有趣的示例,并定义了一个 generic squish_trans 函数,测试起来很有趣。只需适当地调整即可!

    感谢@Stibu 开发和分享squish_trans() 功能。查看R/ggplot2: Collapse or remove segment of y-axis from scatter-plot了解更多详情。

    注意要点:使用scale_y_continuous() 并设置视觉上吸引人的休息时间。

    library(ggplot2)
    
    p<- ggplot(data.frame(), aes(color=c("I","Y","I","Y","I","Y"),     y=c(75.33,72.95,86.46,79.63,1136.09,993.27), x=c(1,1,2, 2,3, 3))) + 
        geom_line() + 
        geom_point()+
        scale_x_continuous(breaks=c(1,2,3),labels = c("A","B","C")) +
    #--------------- apply squish_trans to scale_y_continuous --------------------
    # settings chosen: lower bound = 200, upper bound 900
    # defining breaks to show on graph and 
    # setting limits to "force" a top level tick (this is not strictly necessary, but  looks better ... I think)
        scale_y_continuous( trans  = squish_trans(200, 900, 100)
                           ,breaks = c(0, 50, 100, 900, 1000, 1100, 1200)
                           ,limits = c(0, 1200)
        ) +
    #-------------- remove the additional log-based ticks
    # commented out to show
        #  annotation_logticks(sides = "l")  +
    
        theme(legend.position = "none")+ labs(x = "", y = "")
    
    # print plot
    p
    

    这会产生:

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 2017-10-31
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多