'ERROR while rich displaying an object in ggplot

What am I doing wrong? What does the message mean? (Warning message in rep(yes, length.out = len): “'x' is NULL so the result will be NULL” ERROR while rich displaying an object: Error in ans[ypos] <- rep(yes, length.out = len)[ypos]: replacement has length zero)

Here is my code

plot_tile <- function(country_name_idx, country_names_list, y_lim_ranks=NA) {
  year_breaks <- c(2014, 2016, 2018, 2020, 2022)
  year_strfmt <- function(x) {return(paste("’", str_sub(x, 3, 4), sep="")) }
    }
  
  country_name <- country_names_list['country_name_idx']

  y_title_pct <- if (('country_name_idx %% 3') == 1) "Ranks range" else ""
  y_title_ranks <- if (('country_name_idx %% 3') == 1) "# of places in TOP-500" else ""
  x_title_ranks <- if (('country_name_idx %% 3') == 1) "Year" else ""
      
      
  baseplot_percent <- ggplot(
      filter(ranks_by_year, Country == country_name),
      aes(x = as.factor(Year), y = breaks, fill = counts, color = counts)
    )+
    geom_tile(size=0.3) +
    scale_y_reverse(
      breaks = seq(50, 500, by=50)
    )+
    coord_cartesian(ylim = c(500, 50)) +
    scale_fill_stepsn(
      n.breaks = 5,
      colors = brewer.pal(5, "PuRd"),
      limits = range(c(0, 50)), show.limits = TRUE,
      na.value = "transparent"
    )+
    scale_color_stepsn(
      n.breaks = 5,
      colors = brewer.pal(5, "PuRd"),
      limits = range(c(0, 50)), show.limits = TRUE,
      na.value = "transparent"
    ) +
    scale_x_discrete(
      label = 'year_strfmt',
      breaks = 'year_breaks'
    )+
    guides(
      fill = guide_coloursteps(title.position = "top"),
      color = "none"
    )+
    theme_ipsum_ps(grid="Y", axis="") +
    theme(
      plot.margin = margin(35, 20, 5, 0, "pt"),
      plot.title = element_text(family = "IBMPlexSans-Medium", size = 12),
      axis.title.x = element_blank(),
      axis.text.x = element_blank(),
      axis.ticks = element_blank(),
      legend.direction = "verical",
      legend.margin = margin(0, 0, 0, 15, "pt")
    )+
    labs(
      title = country_name,
      y = y_title_pct,
      x = NULL,
      fill = "Ranked %"
    )
baseplot_ranks <- ggplot(
      filter(top_scores, Country == country_name),
      aes(x = Year, y = RankCount, color = Source)
    )+
    geom_point()+
    geom_line(size = 0.4)+
    scale_color_brewer(palette = "Set2")+
    theme_ipsum_ps(grid="XY", axis="")+
    scale_x_continuous(
        label = 'year_strfmt',
        breaks = 'year_breaks'
    ) +
    expand_limits(y = c(0, 'y_lim_ranks')) +
    guides(
      color = guide_legend(title.position = "top")
    )+
    theme(
      plot.margin = margin(0, 20, 15, 0, "pt"),
      plot.title = element_blank(),
      plot.subtitle = element_blank(),
      legend.direction = "verical",
      legend.margin = margin(100, 0, 0, 15, "pt")
    )+
    labs(
      x = x_title_ranks,
      y = y_title_ranks,
      color = "Ranking"
    )

baseplot_tile <- wrap_plots(
    baseplot_percent,
    baseplot_ranks,
    ncol = 1, nrow = 2
  )

return (baseplot_title)

Warning message in rep(yes, length.out = len): “'x' is NULL so the result will be NULL” ERROR while rich displaying an object: Error in ans[ypos] <- rep(yes, length.out = len)[ypos]: replacement has length zero



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source