Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions