Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Quasiquotation Issue Within ggplot

$
0
0

To simplify reporting on a project, I created a function that DRYs up my ggplots. However, I'm having trouble with bar plots that have both negative and positive results. I tried to write an ifelse statement to change the alignment based on the value of the response variable, but I keep getting an error (Error: Base operators are not defined for quosures). I'm unquoting this variable successfully within the same statement.

library(tidyverse)
library(scales)

bar_chart_by_brand <- function(data, 
                               response_var, 
                               grouping_var, 
                               title="", 
                               subtitle="",
                               caption="",
                               ylab = "Index",
                               hline = 100,
                               round = 0,
                               percent = FALSE,
                               text_col = "white") {
  response_var <- enquo(response_var)
  grouping_var <- enquo(grouping_var)

  p <- ggplot2::ggplot(data, 
                       aes(x = fct_reorder(brand, !!response_var), 
                           y = !!response_var, 
                           fill = !!grouping_var)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_text(aes(label = ifelse(percent == TRUE,
                                 scales::percent(!!response_var),
                                 round(!!response_var, round))), 
              color = text_col,
              fontface = "bold",
              position = position_dodge(width = 1),
              hjust = ifelse(!!response_var >= 0, 1, 0)) +
    coord_flip() +
    scale_fill_discrete() +
    geom_hline(yintercept= hline, color = "#E32D40", alpha = 0.4) +
    labs(title = title,
         subtitle = subtitle,
         caption = caption,
         x = "Brand",
         y = ylab) +
    theme(
      panel.grid.major = element_blank()
    )

  return(p)
}

set.seed(9929)

samp_set <- tibble(brand = sample(c("Alpha", "Beta", "Charlie"), size = 100, replace = TRUE), score = runif(100, min = -10, max = 10), segment = sample(c("Consumer", "Business"), size = 100, replace = TRUE))

samp_set %>% 
  group_by(brand, segment) %>% 
  summarise(rating = mean(score)) %>% 
  bar_chart_by_brand(., response_var = rating, grouping_var = segment, hline = mean(samp_set$score), text_col = "black", ylab = "Mean", round = 1)

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>