R -如何根据Y轴的值改变线的颜色?

hkmswyz6  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(172)

我正在尝试根据Y轴值更改折线图中某条线的颜色。

**我的具体应用:**我正在可视化风速和风向。理想情况下,我会使用玫瑰图来完成此操作,但我会将其与线图中显示的其他数据进行比较。我目前的风向以度为单位,但很难快速解释。我希望根据基本方向(N、NE、E、SE、S、SW、W、NW)将该线分为8个不同颜色的类别,以便更容易理解风向。不要在意图中可能出现的压倒性颜色数量,我只想给予一下,因为我目前拥有的数据很难解释。

我对R非常生疏,没有配备适当的术语来搜索,也不知道从哪里开始。
这是可能的吗?是否需要将序列分解成许多不同的线段?
R甚至Excel中的解决方案将受到赞赏。
编辑:下面是数据外观的示例

# random wind speed and direction over 24 hours, hourly
set.seed(123)

date_time <- seq.POSIXt(ISOdate(2023,1,20, hour = 00), ISOdate(2023, 1, 20, hour = 23), "hour")
wind_speed <- runif(24, 0, 20)
wind_direction <- runif(24, 0, 359)
df_wind <- data.frame(date_time, wind_speed, wind_direction)
mm5n2pyu

mm5n2pyu1#

离散时间x轴的概念使这一点有点挑战性,因为,例如,在每个单位时间内,风向可能在所有方向上变化,这会使混合色线--甚至是分段色线--看起来相当混乱。
一个替代选项将是在背景中放置表示方向的条带,例如:

下面的代码使用rectadjustcolor创建透明条带,我还创建了一个名为directs的向量来简化方向类别。

## Identify directions
directs <- setNames(seq(0, 315, by = 45),
                    c("North", "North-east", "East",
                      "South-east", "South", "South-west", 
                      "West", "North-west"))
# Initiate blank plot
plot(df_wind$date_time, df_wind$wind_direction, type = "n",
     bty = "n", xlab = "Time", ylab = "Direction",
     ylim = c(0, 360))
# Plot bands and text
rect(xleft = min(df_wind$date_time), xright = max(df_wind$date_time),
       ybottom = directs, ytop = c(directs[-1], 360),
       col = adjustcolor(1:8, alpha.f = 0.2), border = NA)
text(names(directs), pos = 4,
       x = min(df_wind$date_time), y = directs + 22.5, col = 1:8)
text(names(directs), pos = 2,
       x = max(df_wind$date_time), y = directs + 22.5, col = 1:8)
# Plot lines over bands
lines(df_wind$date_time, df_wind$wind_direction)

另一种选择是绘制点的颜色,而不是线的颜色,如下所示:

为此,我在数据集df_wind$direction中创建了一个新变量:

df_wind <- dplyr::mutate(df_wind, direction = dplyr::case_when(
  wind_direction <= 45 ~ "North",
  wind_direction > 45 & wind_direction <= 90 ~ "North-east",
  wind_direction > 90 & wind_direction <= 135 ~ "East",
  wind_direction > 135 & wind_direction <= 180 ~ "South-east",
  wind_direction > 180 & wind_direction <= 225 ~ "South",
  wind_direction > 225 & wind_direction <= 270 ~ "South-west",
  wind_direction > 270 & wind_direction <= 315 ~ "West",
  wind_direction > 315 ~ "North-west"
))

dirs <- names(directs)
  
plot(df_wind$date_time, df_wind$wind_direction, type = "l",
       bty = "n", xlab = "Time", ylab = "Direction",
       ylim = c(0, 360))
for(i in seq_along(unique(df_wind$direction))){
  points(df_wind$date_time[df_wind$direction == dirs[i]], 
        df_wind$wind_direction[df_wind$direction == dirs[i]], 
        pch = 21, bg = i)
}
legend("topright", dirs, pch = 21, pt.bg = 1:8, bty = "n", ncol = 2)

或者为什么不两者兼顾?

plot(df_wind$date_time, df_wind$wind_direction, type = "l",
       bty = "n", xlab = "Time", ylab = "Direction",
       ylim = c(0, 360))
rect(xleft = min(df_wind$date_time), xright = max(df_wind$date_time),
     ybottom = directs, ytop = c(directs[-1], 360),
     col = adjustcolor(1:8, alpha.f = 0.2), border = NA)
for(i in seq_along(unique(df_wind$direction))){
  points(df_wind$date_time[df_wind$direction == dirs[i]], 
        df_wind$wind_direction[df_wind$direction == dirs[i]], 
        pch = 21, bg = i)
}
legend("topright", dirs, pch = 21, pt.bg = 1:8, bty = "n", ncol = 2)

最后,您可以根据最近的位置对行进行着色(并添加彩色点-要删除它们,只需删除points循环),如您的评论中所述,但您再次可以看到混乱。

plot(df_wind$date_time, df_wind$wind_direction, type = "n",
     bty = "n", xlab = "Time", ylab = "Direction",
     ylim = c(0, 360))
for(i in 1:nrow(df_wind)){
  if(i != nrow(df_wind)){
    lines(df_wind$date_time[i:(i + 1)], df_wind$wind_direction[i:(i + 1)],
          col = grep(df_wind$direction[i+1], names(directs)))
  }
for(i in seq_along(unique(df_wind$direction))){
  points(df_wind$date_time[df_wind$direction == dirs[i]], 
         df_wind$wind_direction[df_wind$direction == dirs[i]], 
         pch = 21, bg = i)
}
}

相关问题