gpt4 book ai didi

r - 将自定义颜色渐变映射到 POSIXct 值

转载 作者:行者123 更新时间:2023-12-01 11:21:25 26 4
gpt4 key购买 nike

数据:

df1 <- structure(list(Index = 1:11, Duration = structure(c(1487577655, 
1487577670, 1487577675, 1487577680, 1487577685, 1487577680, 1487577700,
1487577705, 1487577695, 1487577700, 1487577680), class = c("POSIXct",
"POSIXt"), tzone = "")), .Names = c("Index", "Duration"), class = "data.frame", row.names = 3:13)

现在我构造图如下:

g1 <- ggplot(df1, aes(x = Index, y = Duration, color = Duration))+
geom_point()+
geom_line()+
scale_y_datetime(labels = date_format("%M:%S"))

就像现在一样,色标设置为默认的“黑色”到“蓝色”渐变。

问题是,我在尝试为数据分配自定义梯度时遇到错误。

对于非 POSIXct 对象:

scale_color_gradient("Duration", low = "#D80427", high = "#07a0ff", space = "Lab")

有效,但我收到以下错误,将 POSIXct 对象 df1$Duration 作为解释变量:

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

绘制 POSIXct 对象时是否需要使用不同的梯度函数?

最佳答案

你可以使用trans = time_trans():

library(ggplot2)
library(scales)
g1 +
scale_color_gradient("Duration", low = "#D80427", high = "#07a0ff",
trans = time_trans())

enter image description here

如果您希望图例中的标签采用另一种格式,请添加例如labels = format(pretty(df1$Duration), "%M:%S")

关于r - 将自定义颜色渐变映射到 POSIXct 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42354528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com