2016-12-16 32 views
0

假设我们有一个系列在2D空间中的位置的随时间变化:改变与POSIXt连续色标对象

start.time <- strptime("2016-11-22_15-44-24", 
         format = "%Y-%m-%d_%H-%M-%S", 
         tz = "UTC") 

end.time <- strptime("2016-11-22_17-25-12", 
        format = "%Y-%m-%d_%H-%M-%S", 
        tz = "UTC") 
date <- seq.POSIXt(from = start.time, to = end.time, length.out = 100) 
a <- seq(0, 10, length.out = 100) 
x <- a * cos(a) 
y <- a * sin(a) 
my.df <- data.frame(date, x, y) 

ggplot(my.df, aes(x, y, color = date)) + geom_point() 

这给了下面的图:

enter image description here

现在我想将颜色调色板更改为更“动态”的颜色调色板,例如颜色调制器的调色板。

this answer,我用命令:

myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral"))) 
sc <- scale_colour_gradientn(colours = myPalette(100)) 

ggplot(my.df, aes(x, y, color = date)) + geom_point() + sc 

但我有一个错误:

Error in Ops.POSIXt((x - from[1]), diff(from)) : '/' not defined for "POSIXt" objects

(从实际的错误,我得到的,这是一半英语/半法文粗略的翻译) 。

我想在某些时候,scale_color_gradientn试图将我的时间尺度分成相等的部分,但不能这样做,因为它不知道如何划分日期。

那我该怎么办?

回答

2

,你可以做一个小数字变通这样与scale_color_distiller

library(lubridate) 
ggplot(my.df, aes(x, y, color = as.numeric(date))) + 
    geom_point() + 
    scale_color_distiller(palette = "Spectral", 
          breaks = as.numeric(my.df$date[c(1,50,100)]), 
          labels = paste0(hour(my.df$date[c(1,50,100)]), ":", minute(my.df$date[c(1,50,100)])), 
          name = "date") 

enter image description here

scale_color_brewer将只用于离散日期

工作