【问题标题】:How to use Infinity limits with a transformed scale?如何使用具有变换比例的无限极限?
【发布时间】:2019-02-21 15:31:53
【问题描述】:

当将转换应用于 y 轴时,如果想要将 geom_rect 应用于 ggplot 对象的 y 轴上的 Infinity,是否有解决方法?

除非您注释掉 scale_y_continuous 线,否则下面的代码不会绘制间隔矩形。使用转换后的比例时,我必须输入实际数据限制。我可能会编写一个函数来查找正在绘制的其他数据的最小值/最大值以避免硬编码值,但我正在寻找更接近 Inf 方法的东西。我尝试使用 NA 而不是 Inf 但没有运气。

    library(tidyverse)
    data(economics)

    ints<-data.frame(start=as.Date(paste0(seq(1970,2020,by=10),"-01-01"))) %>% 
      mutate(end=start+1785)

    plt<-ggplot(economics,aes(date,unemploy)) + theme_bw() +
      scale_y_continuous(trans="sqrt") +
      geom_rect(data=ints,inherit.aes=F,aes(xmin=start,xmax=end,ymin=-Inf,ymax=Inf)) + geom_line()

    plt

【问题讨论】:

  • 因为你不能取负数的平方根,所以它不能处理ymin =-Inf。为什么不直接设置ymin=0

标签: r ggplot2


【解决方案1】:
library(tidyverse)
data(economics)

ints<-data.frame(start=as.Date(paste0(seq(1970,2020,by=10),"-01-01"))) %>% 
  mutate(end=start+1785)

plt<-ggplot(economics,aes(date,unemploy)) + theme_bw() +
  scale_y_continuous(trans="sqrt") +
  geom_rect(data=ints,inherit.aes=F,aes(xmin=start,xmax=end,ymin=0,ymax=Inf)) + geom_line()+
  coord_cartesian(ylim=c(2000,20000)) # This will allow you to control how zoomed in you want the plot

plt

【讨论】:

  • 哦。谢谢。我会删除我自己的问题(或至少为它投票),如果它不会让我面临将来无法提问的风险。
  • 老实说,这是一个显而易见的答案,但我相信这个帖子会对新手有所帮助。
猜你喜欢
  • 2015-03-04
  • 2020-11-08
  • 1970-01-01
  • 2012-11-27
  • 2017-04-30
  • 1970-01-01
  • 2016-01-18
  • 1970-01-01
  • 2015-08-19
相关资源
最近更新 更多