基于值ggplot2

时间:2018-02-28 19:28:07

标签: r ggplot2 longitudinal

我正在尝试使用ggplot2自定义颜色。我写的函数如下:

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'))
}

然而,这并不起作用,但我似乎无法弄清楚为什么scale_color_manual(..)值不起作用。我目前使用的情节是:

enter image description here

这与我想要实现的目标有些一致:值为1-3的暗色(即基于first_answer的颜色为1到5),而较轻的颜色为4和5。只是因为还有更多的行,其值为4或5,我希望能够看到时间线的方向。

编辑图片是我目前的情节。虽然它有点类似于我想要的东西,但我更喜欢自己设置颜色或者使用一些选择颜色的功能来自动增强绘图可见性(图中的线条)。

1 个答案:

答案 0 :(得分:2)

您可以使用'scale_x_gradient'scale_x_gradient2scale_x_gradientn指定颜色渐变 (x可以是fillcolor

使用values = c(...)指定颜色值时发出警告:values()根据c(0,1)中的位置指定颜色。因此,您需要将您想要作为中断的向量中的值缩放到范围c(0,1)。

回答你的问题哪个调色板最适合用于5个不同的行:我认为最好是手动指定颜色。我经常使用十六进制代码。我个人看看那些人 html color codes