我想在ggplot2中创建一个路径图,路径基于时间序列,并且连续的颜色美学来显示这条路径 . 图例应将颜色与时间序列相关联 . 在对ggplot的原始调用中定义颜色美学时,图例很好:
library(tidyverse)
times <- seq.POSIXt(from = as.POSIXct("2017-10-01 02:00:00"),
to = as.POSIXct("2017-10-03 12:34:00"),
by = "min")
values <- rnorm(length(times))
dat <- data.frame(times = times,
values1 = sin(values[order(values)]),
values2 = cos(values[order(-values)]))
ggplot(dat, aes(values1, values2, color=times)) +
geom_path()
但是,我想使用与默认颜色不同的颜色比例 . 使用 scale_color_gradientn
或 scale_color_continuous
时,图例条目似乎从POSIXct转换为整数:
ggplot(dat, aes(x = values1, y = values2, color = times)) +
geom_path() +
scale_color_gradientn(colors = rainbow(4))
How do I either (1) specify a custom color scale in the first plot, or (2) maintain POSIXct legend entries in the 2nd plot?
1 回答
查看默认使用的
scale_colour_datetime()
,它所做的只是:因此,将
trans = "time"
添加到所需的色阶可以:输出: