【问题标题】:How to mix aes_() and arithmetic calculation in ggplot2?如何在ggplot2中混合aes_()和算术计算?
【发布时间】:2018-06-11 13:22:04
【问题描述】:

我正在尝试在Rggplot2 包中的以下脚本中调整xymax 以调整errorbar 的绘图坐标,但是它返回错误。

  gplot <- function(prd) {

        ggplot() +
        geom_polygon(data=shp.t,aes(x=long,y=lat,group=group),
                      fill="white",colour="grey") +
        ## Plot errorbar
        geom_errorbar(data=te10.cent,size=2,colour="red",
                      alpha=.8,width=0,
                      aes_(x=quote(long.cent)-350,ymin=quote(lat.cent),       
                                 ymax=quote(lat.cent)+prd))
        }
gplot("Field Name") # Not number but field name of the data frame

(抱歉,我无法上传我正在使用的实际数据框。)

这些是我面临的错误:

Error in quote(lat.cent) + prd * .pt : non-numeric argument to binary Operator
Error in quote(lat.cent)+prd * .pt : non-numeric argument to binary Operator

如果从脚本中省略 -350+prd 或在数据框中使用带有实际变量的“aes”,则它在这两种情况下都有效。 我尝试了其他脚本; "long.cent"-350"lat.cent"+prd 而不是上面的脚本,但是它也会返回相同的错误。

我搜索了解决方案,但所有解决方案都解释了如何在不混合参数和算术计算的情况下使用 aes_。我需要将非标准表达式与算术计算混合来调整我的情节,但是如何?

【问题讨论】:

  • 您是否尝试过使用scale_x_continuous / scale_y_continuouscoord_cartesian?看到这个帖子stackoverflow.com/questions/3606697/…
  • 你能创建一个可重现的例子吗?在诸如汽车或虹膜之类的数据集中构建之一?
  • 感谢您的两个建议,感谢@missuse 的解决方案。

标签: r ggplot2 nse aesthetics


【解决方案1】:

我相信这会解决您的问题:

gplot <- function(prd) {

  ggplot() +
    geom_polygon(data = shp.t,
                 aes_(x = ~long,
                      y = ~lat,
                      group = ~group),
                 fill = "white",
                 colour = "grey") +
    ## Plot errorbar
    geom_errorbar(data = te10.cent,
                  size = 2,
                  colour = "red",
                  alpha = .8,
                  width = 0,
                  aes_(x = ~long.cent - 350,
                       ymin = ~lat.cent,       
                       ymax = ~lat.cent + prd))
}

可重现的例子:

一些数据:

library(tidyverse)

data(iris)

iris %>%
  group_by(Species) %>%
  summarise_all(~mean(.)) -> summed_iris

gplot <- function(prd){
  ggplot(summed_iris) +
    geom_col(aes_(x = ~Species,
                  y = ~Sepal.Length))+
    geom_errorbar(aes_(x = ~Species,
                       ymin = ~Sepal.Length -prd,
                       ymax = ~Sepal.Length + prd))

}

gplot(0.5)

编辑:评论中的问题:

如果prd 是数据的列名,最好预先计算值:

gplot <- function(prd){
  ymin <-  with(summed_iris, get("Sepal.Length") - get(prd))
  ymax <-  with(summed_iris, get("Sepal.Length") + get(prd))
  summed_iris <- data.frame(summed_iris, ymin, ymax)
  ggplot(summed_iris) +
    geom_col(aes_(x = ~Species,
                  y = ~Sepal.Length))+
    geom_errorbar(aes_(x = ~Species,
                       ymin = ~ymin,
                       ymax = ~ymax))

}
gplot("Petal.Length")

【讨论】:

    【解决方案2】:

    随着即将推出的 ggplot2 2.3.0(将于 2018 年 6 月下旬发布),aes_(...) 已被软性弃用,建议使用 tidy eval。这意味着在此处的上下文中,当您想要变量 prd 的内容而不是符号 prd 时,您编写 !!prd

    library(ggplot2)
    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    
    iris %>%
      group_by(Species) %>%
      summarise_all(~mean(.)) -> summed_iris
    
    gplot <- function(prd){
      ggplot(summed_iris) +
        geom_col(aes(x = Species, y = Sepal.Length)) +
        geom_errorbar(
          aes(x = Species, ymin = Sepal.Length - !!prd, ymax = Sepal.Length + !!prd)
        )
    }
    
    gplot(0.5)
    

    如果您希望函数gplot() 能够像aes() 那样接受符号,那么您需要在函数定义的开头添加enquo()。它捕获提供给函数的表达式以及封闭环境。

    gplot <- function(prd){
      prd <- enquo(prd)
      ggplot(summed_iris) +
        geom_col(aes(x = Species, y = Sepal.Length)) +
        geom_errorbar(
          aes(x = Species, ymin = Sepal.Length - !!prd, ymax = Sepal.Length + !!prd)
        )
    }
    
    gplot(Petal.Length)
    

    scale <- 1/5
    gplot(scale*Petal.Length)
    

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 2011-09-06
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      相关资源
      最近更新 更多