简体   繁体   English

基于值ggplot2的深色到浅色

[英]Dark to light colours based on value ggplot2

I am trying to customize the colours using ggplot2. 我正在尝试使用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. 但是,这不起作用,但是我似乎无法弄清为什么scale_color_manual(..)值不起作用。 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. 这与我要达到的目的有些一致:值为1-3的深色(即基于first_answer ,范围为1到5),为4和5的颜色较浅。原因仅仅是因为还有更多线的值是4或5,我希望能够看到线在时间上的方向。

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 ) 您可以使用' scale_x_gradient2scale_x_gradientn (x可以是fillcolor )指定颜色渐变。

Caveat when specifying the color values with values = c(...) ): values() assigns colours based on their position within c(0,1) . 在指定values = c(...)的颜色值时需要注意: values()根据颜色在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). 因此,您需要将向量中要中断的值缩放到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. 再问一个问题,哪个调色板最适合用于5条不同的线:我认为最好是在完成操作后手动指定颜色。 I often use hex codes instead. 我经常使用十六进制代码代替。 I personally look those up at 我亲自看那些
html color codes . html颜色代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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