简体   繁体   中英

Dark to light colours based on value ggplot2

I am trying to customize the colours using ggplot2. The function I wrote is as follows:

library(tidyverse)
spaghetti_plot_multiple <- function(input, MV, item_level){
  MV <- enquo(MV)
  titles <- enquo(item_level)
  input %>% 
    filter(!!(MV) == item_level) %>% 
    mutate(first_answer = first_answer) %>%
    ggplot(.,aes( x = time, y = jitter(Answer), group = ID)) +
    geom_line(aes(colour = first_answer)) +
    labs(title = titles ,x = 'Time', y = 'Answer', colour = 'Answer given at time 0') +
    facet_wrap(~ ID, scales = "free_x")+ 
    theme(strip.text = element_text(size = 8)) + 
    scale_color_manual(values = c('red', 'blue', 'brown', 'purple', 'black'))
}

This however doesn't work, but I can't seem to figure out why scale_color_manual(..) values doesn't work. The current plot I am using is:

在此处输入图片说明

This is somewhat in line with what I am trying to achieve: a dark color for values 1-3 (ie based on first_answer which ranges from 1 to 5) and lighter ones for 4 and 5. The reason is simply because there are many more lines with a value of 4 or 5 and I want to be able to see the direction of lines across time.

EDIT The image is the plot I currently have. Although it somewhat resembles what I'd like to get, I'd much rather set the colors myself or use some function that chooses colors to enhance the plotting visibility (the lines in the plot) automatically.

You can specify color gradients with 'scale_x_gradient' scale_x_gradient2 or scale_x_gradientn (x can be fill or color )

Caveat when specifying the color values with values = c(...) ): values() assigns colours based on their position within c(0,1) . You therefore need to scale the values from your vector which you want to have as breaks to the range c(0,1).

Re your question which palette best to use for 5 distinct lines: I think best is to manually specify the colours as you have done. I often use hex codes instead. I personally look those up at
html color codes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM