【发布时间】:2020-05-09 02:05:38
【问题描述】:
x=rnorm(6000, 10, 4)
plot(x, type = "l")
对于索引0-2000,我想使用green color,下一个2000-4000 red 和最后一个4000-6000 blue color。
如何用多种颜色为该图着色?
【问题讨论】:
x=rnorm(6000, 10, 4)
plot(x, type = "l")
对于索引0-2000,我想使用green color,下一个2000-4000 red 和最后一个4000-6000 blue color。
如何用多种颜色为该图着色?
【问题讨论】:
这是一个 ggplot 解决方案:
library(tidyverse)
df <-
x %>%
enframe(name = "Index") %>%
mutate(
color = case_when(
Index <= 2000 ~ "green",
Index <= 4000 ~ "red",
TRUE ~ "blue"
)
)
df %>%
ggplot(aes(x = Index, y = value, color = color)) +
geom_line(show.legend = FALSE) +
scale_color_manual(values = c("blue" = "blue", "green" = "green", "red" = "red"))
【讨论】: