I would like to format my rows using a vector of colors, however when I provide it the colors it only uses the first color in the vector. How can I apply each color of the vector to the corresponding rows?
This is what I mean by corresponding rows:
> which(all_table_df$`Theme Group` == "Positive")[1] 1 2 3 4 8 9> gradient_colors_p[1] "#004750" "#29656C" "#538488" "#7DA3A5" "#A8C2C1" "#D2E1DE"
Data:
> dput(all_table_df)structure(list(Themes = structure(c(9L, 7L, 10L, 8L, 2L, 1L, 3L, 11L, 12L, 5L, 6L, 4L), levels = c("Boredom or irritation with the advertising approach", "Confusion about the advertisement's message or product being promoted", "Disinterest in sports/soccer or the World Cup","Dislike for the portrayal of alcohol consumption in relation to sports", "Negative reactions to the incorporation of tattoos and extreme promises", "Non-thematic Negativity", "Excitement for the World Cup", "Humor and Entertainment", "Non-thematic Positivity", "Positive Emotions and Enjoyment", "Relevance and Engagement", "Visual and Musical Appeal"), class = "factor"), `Theme Group` = c("Positive", "Positive", "Positive", "Positive", "Negative", "Negative", "Negative", "Positive", "Positive", "Negative", "Negative", "Negative"), `Percentage of Total Sample` = c(15.99, 13.82, 7.73, 7.15, 6.81, 5.85, 4.93, 4.35, 3.04, 2.17, 1.06, 1.06)), row.names = c(NA, -12L), class = "data.frame")
Code:
# Define the color rampgradient_colors_p <- rev(colorRampPalette(c("#D2E1DE", "#004750"))(length(which(all_table_df$`Theme Group` == "Positive"))))gradient_colors_n <- rev(colorRampPalette(c("#FF9A98", "darkred"))(length(which(all_table_df$`Theme Group` == "Negative"))))# Apply the colors to the specified rowsall_table_df %>% kable(format = "html", escape = FALSE) %>% kable_styling(full_width = FALSE) %>% row_spec(which(all_table_df$`Theme Group` == "Positive"), background = gradient_colors_p, color = "white") %>% row_spec(which(all_table_df$`Theme Group` == "Negative"), background = gradient_colors_n, color = "white")