Plotting map() output for dummies. - includes reprex

Hi there,

In order to test my skills I tried tried to create a simple function and with the output make a plot.

The function I thought of was very simple, I want to visualize revenue given two input variables, n_customers & price both vectors.

# Setup Libs
library(tidyverse)

# Setup Revenue Function 
revenue_projection <- function(n_customers= 100,price = 1){
  revenue <- n_customers * price * 12
  return(revenue)
} 

# Customer & License Price Iteration variables
n_customers <- seq(60,600,60)
price <- seq(150,185,5)

#So I'm able to create the data frame and utilize the function correctly:
price %>% purrr::map(revenue_projection,n_customers) %>% head(3)
#> [[1]]
#>  [1]  108000  216000  324000  432000  540000  648000  756000  864000  972000
#> [10] 1080000
#> 
#> [[2]]
#>  [1]  111600  223200  334800  446400  558000  669600  781200  892800 1004400
#> [10] 1116000
#> 
#> [[3]]
#>  [1]  115200  230400  345600  460800  576000  691200  806400  921600 1036800
#> [10] 1152000

Created on 2021-06-24 by the reprex package (v2.0.0)

My issue was not understanding how to "cycle"/ "for loop" ability with ggplot()

Here is what I thought of:

# My issue was with the plotting;
# Plot x = n_customers vs y = revenue_projection, price by color.

price %>% ggplot(
  aes(x = n_customers,
      y = purrr::map(revenue_projection,n_customers),
      color = price
)) + geom_line()
#> Error in price %>% ggplot(aes(x = n_customers, y = purrr::map(revenue_projection, : could not find function "%>%"

Created on 2021-06-24 by the reprex package (v2.0.0)

Any insight would be greatly appreciated and thank you for your time!

As a rule I choose to create my data in code separate from my plotting code. I believe it creates cleaner, and more adaptable code.

library(tidyverse)

# Setup Revenue Function 
revenue_projection <- function(n_customers= 100,price = 1){
  revenue <- n_customers * price * 12
  return(revenue)
} 


(input_data <- expand_grid(n_customers = seq(60,600,60),
                           price = seq(150,185,5)) %>% mutate(
  revenue = revenue_projection(n_customers,price)
))


ggplot(input_data,
  aes(x = n_customers,
      y = revenue,
      group = price,
      color=price
  )) + geom_line()

1 Like

@nirgrahamuk

Firstly thank you very much for your assistance! I'll put into practice your suggestion of separating data and plotting moving on, thank you!

If you don't mind, if I may, is there is a specific reason why you chose to use expand_grid() to create the tibble() as well as for the general bracket encapsulating input_data all the way to the end, or were you just emphasizing that that was the data portion followed by the plot?

Thank you for your feedback, helps a lot!

Ggplot2 wants to take a data.frame for its data argument. There are several ways to create that, your example seemed suited for expand grid

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.