R-ggplot2
1. 简单的代码
这个代码是我随便从网上找到,然后修改的。
library(tidyverse)
mydata <- data.frame(x = seq(-3, 3, 1e-2)) %>%
mutate(y1 = cos(x)) %>%
mutate(y2 = cos(x) + sin(x)) %>%
mutate(color_value = sin(x))
mydata
mydata %>% ggplot() +
geom_segment(aes(x=x, y = y1, xend=x,yend=y2, color=color_value)) +
scale_colour_gradient2(low=scales::muted("blue", l=60),
mid=scales::muted("green", l=60),
high=scales::muted("red", l=60)) +
theme_bw()
2. 定制代码
大哥说,他希望可以这样:颜色只有两