【问题标题】:Dynamic ylim in ggplot2 using dplyr pipeggplot2中使用dplyr管道的动态ylim
【发布时间】:2015-05-21 05:36:22
【问题描述】:

我想在 ggplot 中创建动态 ylim 值,以便 ylim 参数引用 dplyr 通过管道提供的值。为了说明问题,请查看我想将其更改为(当前无法正常工作的通用)代码的工作(非通用)代码。

require(dplyr)
require(scales)
require(ggplot2)

x <- data.frame(name = c("A","B","C"), 
                value = c(2,4,6))

工作的非通用代码:

arrange(x[1:2, ], value) %>%
  ggplot(data=., aes(x=factor(name), y=value)) + 
  geom_bar(stat="identity") +
  scale_y_continuous(labels=comma, 
                     limits=c(0,max(arrange(x[1:2, ], value)$value) * 1.1))

通用代码不起作用(调用未找到值):

arrange(x[1:2, ], value) %>%
  ggplot(data=., aes(x=factor(name), y=value)) + 
  geom_bar(stat="identity") +
  scale_y_continuous(labels=comma, 
                     limits=c(0,max(value) * 1.1))

所以问题是是否有任何方法可以设置限制,即排列后的部分总是相同的(我需要生成许多具有不同 x 的相同图表,即不同的限制)。 谢谢!

【问题讨论】:

    标签: r ggplot2 dplyr limits


    【解决方案1】:

    如果您可以使用 &lt;&lt;- 并且不介意使用一个额外的变量,以下将保留管道并为您提供动态的 ylim

    max_val <- 0
    arrange(x[1:2, ], value) %>%
    { max_val <<- max(.$value) 
    . } %>% 
      ggplot(data=., aes(x=factor(name), y=value)) + 
      geom_bar(stat="identity") +
      scale_y_continuous(labels=comma, 
                         limits=c(0, max_val * 1.1))
    

    【讨论】:

    • 有没有办法让“max_val
    • 是的。如果您在脚本顶部附近定义它,它将位于命名空间/环境中(这对于 &lt;&lt;- 工作而言非常重要),那么每次在任何管道中执行 {} 块时,它都会只需分配给该范围变量。无需每次都发送&lt;- 0
    • 我这两天一直在寻找这个答案。谢谢。我认为 {} 来自 magrittr,我的假设是否正确?
    • .} %>% 似乎只有在单独的行上才有效。否则不会。
    【解决方案2】:

    您可以在极限值处绘制一个大小为零的虚拟点。这不是一个特别漂亮的解决方案,但它似乎相当灵活。代码看起来像

    arrange(x[1:2, ], value) %>%
      ggplot(data=., aes(x=factor(name), y=value)) + 
      geom_bar(stat="identity") +
      geom_point(aes(x=c(name[1], name[1]), y = c(0, max(value)*1.1)), size=0) +
      scale_y_continuous(labels=comma)
    

    【讨论】:

    • 这是一个很好的解决方法,尽管我还没有想到任何缺点。在这个解决方案中我仍在努力解决的一件事是我的表并不总是有一个名为“名称”的列。它总是会有所不同,例如名称 1、名称 2 等
    【解决方案3】:

    如果您调用max(x$value[1,2]),您可以避免让ggplot 将arg 解析为max()。 您的代码也没有解释 comma 所以我把它排除在答案之外。

    arrange(x[1:2, ], value) %>%     
        ggplot(data=., aes(x=factor(name), y=value)) + 
        geom_bar(stat="identity") +
        scale_y_continuous(limits=c(0,max(x$value[1:2])) * 1.1)
    

    【讨论】:

    • 我不确定我是否理解这个答案。想法是从 scale_y_continuous 调用中取出“x”?
    • 无论您在对arrange() 的调用中对子集x 做什么,在对max() 的调用中使用相同的子集。
    【解决方案4】:

    尝试了这两种解决方案

    arrange(x[1:3, ], value) %>%
        ggplot(data=.,
               aes(x=factor(name),
                   y=value)) +
        geom_bar(stat="identity") +
        scale_y_continuous()
    
    arrange(x[1:2, ], value) %>%
        ggplot(data=., aes(x=factor(name), y=value)) +
        geom_bar(stat="identity") +
        geom_point(aes(x=c(name[1], name[1]), y = c(0, max(value)*1.1)), size=0) +
        scale_y_continuous()
    

    似乎它(自动)调整?

    代码二的原文

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2019-11-18
      • 1970-01-01
      相关资源
      最近更新 更多