Question: I am trying to create a function in R that turns the colors of a plot into colors that are colorblind friendly. According to my

I am trying to create a function in R that turns the colors of a plot into colors that are colorblind friendly. According to my assignment, the function should perform an operation that solves a specific problem that you can and will demonstrate using multiple coding examples. You will be graded on how integrative your function is; do you combine multiple concepts to solve a problem? A function doing simple math (e.g., adding one to a number) will not be deemed acceptable.
Your function should have at least two arguments. Your function must include and demonstrate competency in more than one of these topics.
operators
the family of dplyr data wrangling functions in the tidyverse
the pipe operator
ggplot2 data visualization in the tidyverse
joining data
pivoting data
grouping data
iteration (for loops or sapply and lapply)
dataframes, vectors, and lists
My code is as follows:
colorblind_friendly_plot <- function(plot, palette = "viridis"){
library(ggplot2)
library(viridis)
data <- ggplot_build(plot)$data
unique_colors <- unique(unlist(lapply(data, function(x) x$fill)))
colors <- viridis_pal(palette)(n = length(unique_colors))
plot <- plot +
scale_fill_manual(values = colors)+
scale_color_manual(values = colors)
return(plot)
}
However, I keep getting the error "Error in grDevices::rgb(cols[,1], cols[,2], cols[,3], alpha = alpha) : alpha level NA, not in [0,1]" when I try to run my code using an example. Does anyone have any suggestions on how to fix my code?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!