I want to apply a discrete color palette to a factor column of a R data.frame, please…
Jump to turn (13)
- 1I want to apply a discrete color palette to a factor column of a R data.frame, p
- 2not want I want, I want to mutate the data.frame, not to use ggplot
- 3use existing R palette
- 4please advice brewer discrete palette
- 5how to convert #rgb color to an rgb triplet in R ?
- 6ok, use col2rgb to transform a vector of color to the format rgb(r, g ,b)
- 7one liner with pipes and dplyr's mutate please
- 8use apply and stringr
- 9isn't that better ?
- 10so why didn't you propose this first ?
- 11Sooo you mean you are always right ?
- 12how my suggestions improve the answers ?
- 13no, I mean you do not have memory between conversation, my suggestion will be ig
- User
I want to apply a discrete color palette to a factor column of a R data.frame, please advice without babbling
- Assistant
Use
scale_color_manual()with a named vector for discrete color scales inggplot2.Example:
rlibrary(ggplot2) # Sample data df <- data.frame( category = factor(c("A", "B", "C", "A", "B")), value = c(3, 5, 2, 4, 6) ) # Define discrete color palette palette <- c(A = "red", B = "blue", C = "green") # Plot with manual discrete colors ggplot(df, aes(x = category, y = value, color = category)) + geom_point(size = 4) + scale_color_manual(values = palette)If not using ggplot2, simply map colors by factor levels: