【问题标题】:Function in R giving error with ggplot outputR中的函数给出ggplot输出错误
【发布时间】:2018-07-17 02:35:31
【问题描述】:

样本数据:

df <- tibble(
 "PLAYER" = c("Corey Kluber", "CLayton Kershaw", "Max Scherzer", "Chris Sale",
           "Corey Kluber", "Jake Arrieta", "Jose Urena", "Yu Darvish"),
 "YEAR" = c(2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017),
 "WHIP" = c(1.24, 1.50, 1.70, 1.35, 1.42, 1.33, 1.61, 1.10),
 "ERA" =  c(3.27, 4.0, 2.56, 1.45, 3.87, 4.23, 3.92, 2.0)
)

数据集要大得多,但我写了一个函数(不起作用)来检索玩家和所需的统计数据,然后使用ggplot 输出一个绘图:

baseball_stats <- function(player, statistic) {

  # Libraries
  library(tidyverse)
  library(rvest)
  library(ggrepel)

  # Function to set YEAR scale to number of seasons played by pitcher
  f <- function(k) {
    step <- k
    function(y) seq(floor(min(y)), ceiling(max(y)), by = step)
   }

  # ggplot of player and chosen statistic
  p <- df %>% 
    group_by(PLAYER) %>% 
    filter(PLAYER == player) %>% 
    ggplot() +
    geom_col(aes(YEAR, statistic), width = .5) +
    scale_x_continuous(breaks = f(1)) +  # Uses the function to set YEAR breaks
    scale_y_continuous(breaks = f(0.1)) +
    theme_bw() +
  coord_flip() +
  labs(
   title = "statistic Statistic: player",
   subtitle = "statistic over seasons played",
   x = "Year",
   y = "statistic Stat",
   caption = "Data from espn.com")

  print(p)
  return(baseball_stats)

}

baseball_stats("Corey Kluber", WHIP)

我得到Error: Discrete value supplied to continuous scale 或另一个关于$ 和原子向量的错误(我的数据集是使用rvest 抓取的,我必须清理它,我试图将它包含在我的函数中)。谢谢

【问题讨论】:

    标签: r function ggplot2


    【解决方案1】:

    aes更改为aes_string后得到了一个情节

    geom_col(aes_string("YEAR", statistic), width = .5)
    

    请注意YEAR 在引号中。我将其称为以下命令

    baseball_stats("Corey Kluber", "WHIP")
    

    WHIP 也是在 quot 中传递的。

    完整代码在这里:

    baseball_stats <- function(player, statistic) {
    
      # Libraries
      library(tidyverse)
      library(rvest)
      library(ggrepel)
    
      # Function to set YEAR scale to number of seasons played by pitcher
      f <- function(k) {
        step <- k
        function(y) seq(floor(min(y)), ceiling(max(y)), by = step)
      }
    
      # ggplot of player and chosen statistic
      p <- df %>% 
        group_by(PLAYER) %>% 
        filter(PLAYER == player) %>% 
        ggplot() +
        geom_col(aes_string("YEAR", statistic), width = .5) +
        scale_x_continuous(breaks = f(1)) +  # Uses the function to set YEAR breaks
        scale_y_continuous(breaks = f(0.1)) +
        theme_bw() +
        coord_flip() +
        labs(
          title = "statistic Statistic: player",
          subtitle = "statistic over seasons played",
          x = "Year",
          y = "statistic Stat",
          caption = "Data from espn.com")
    
      print(p)
      return(baseball_stats)
    
    }
    
    baseball_stats("Corey Kluber", "WHIP")
    

    【讨论】:

    • 太棒了,谢谢!有没有办法将函数传递给 ggplot 的 labs() 部分?谢谢!
    • 我没有收到您关于通过labs() 的问题。如果需要修改函数外的labs()。您可以在函数内部将print(p) 更改为return (p) 并将其存储到变量中,比如myplot。然后你可以做myplot + labs( title = ..., subtitle = ..., ...)。您可以删除函数内的labs() 部分。另外我不知道你为什么在函数内返回baseball_stats。让我知道它是否回答了您的问题。如果是这样,请随时将问题标记为答案。
    • 我不应该使用return()吗?这个问题似乎在它上面分裂:stackoverflow.com/questions/11738823/…
    • 如果您问的是一般问题,该问题已在链接中进行了深入讨论,我个人认为这是一个清晰的问题。但是,在您的情况下,return(baseball_stats),它将函数本身作为对象返回,我不认为您打算这样做。您可以返回 (p) ,其中 p 是一个 ggplot 对象,由 return (p) 显式返回或通过函数中的最后一行以 p 结尾隐式返回。返回后,可以对返回的对象做很多ggplot操作,比如returned_object + lab(...
    • 使用return(p) 实际上加快了函数/图形过程。我很感激!谢谢楼主
    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 2020-08-17
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多