【问题标题】:Change line color depending on y value with ggplot2使用 ggplot2 根据 y 值更改线条颜色
【发布时间】:2016-01-27 14:20:02
【问题描述】:

我对 ggplot2 中的线条颜色有疑问。我需要绘制太阳辐射数据,但我只有 6 小时数据,所以geom_line 不会给出“不错”的输出。我试过geom_smooth,结果接近我需要的。但是我有一个新问题,是否可以根据 y 值更改线条颜色?

用于绘图的代码是

library(ggplot2)
library(lubridate)

# Lectura de datos
datos.uvi=read.csv("serie-temporal-1.dat",sep=",",header=T,na.strings="-99.9")
datos.uvi=within(datos.uvi, fecha <- ymd_h(datos.uvi$fecha.hora))

# geom_smooth
ggplot(data=datos.uvi, aes(x=fecha, y=Rad_Global_.mW.m2., colour="GLOBAL")) +
  geom_smooth(se=FALSE, span=0.3)

在所需的输出中,辐射值低于 250 的线应为红色,250-500 区间为绿色,高于 500 的线应为蓝色。

geom_smooth 可以吗?我尝试重用代码here,但找不到重点。

用于绘图的数据:

dput(datos.uvi)
structure(list(fecha.hora = c(2016012706L, 2016012712L, 2016012718L,
2016012800L, 2016012806L, 2016012812L, 2016012818L, 2016012900L,
2016012906L, 2016012912L, 2016012908L, 2016013000L), latitud = c(37.75,
37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75,
37.75, 37.75), longitud = c(-1.25, -1.25, -1.25, -1.25, -1.25,
-1.25, -1.25, -1.25, -1.25, -1.25, -1.25, -1.25), altitud = c(300L,
300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L
), cobertura_nubosa = c(0.91, 0.02, 0.62, 1, 0.53, 0.49, 0.01,
0, 0, 0.13, 0.62, 0.84), longitud_de_onda_inicial.nm. = c(284.55,
284.55, 284.55, 284.55, 284.55, 284.55, 284.55, 284.55, 284.55,
284.55, 284.55, 284.55), Rad_Global_.mW.m2. = c(5e-04, 259.2588,
5, 100.5, 1, 886.5742, 110, 40, 20, 331.3857, 0, 0), Rad_Directa_.mW.m2. = c(0,
16.58034, 0, 0, 0, 202.5683, 0, 0, 0, 89.81712, 0, 0), Rad_Difusa_.mW.m2. = c(0,
242.6785, 0, 0, 0, 684.0059, 0, 0, 0, 241.5686, 0, 0), Angulo_zenital_.º. = c(180,
56.681, 180, 180, 180, 56.431, 180, 180, 180, 56.176, 180, 180
), blank = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
    fecha = structure(c(1453874400, 1453896000, 1453917600, 1453939200,
    1453960800, 1453982400, 1454004000, 1454025600, 1454047200,
    1454068800, 1454054400, 1454112000), tzone = "UTC", class = c("POSIXct",
    "POSIXt"))), row.names = c(NA, -12L), .Names = c("fecha.hora",
"latitud", "longitud", "altitud", "cobertura_nubosa", "longitud_de_onda_inicial.nm.",
"Rad_Global_.mW.m2.", "Rad_Directa_.mW.m2.", "Rad_Difusa_.mW.m2.",
"Angulo_zenital_.º.", "blank", "fecha"), class = "data.frame")

提前致谢。

【问题讨论】:

  • 我效率不高,但也许您可以创建您想要(重新)着色的区域的子集,然后添加 geom_line(data=subset....) 或在您当前的数据集将为您希望以不同方式着色的某些区域分配颜色
  • 您可以查看Conditional colouring of a geom_smooth。您需要根据休息时间调整颜色条件 (color = y &gt; 0),并“照顾”y = 0...

标签: r ggplot2


【解决方案1】:

计算ggplot2外的平滑,然后使用geom_segment

fit <- loess(Rad_Global_.mW.m2. ~ as.numeric(fecha), data = datos.uvi, span = 0.3)
#note the warnings

new.x <- seq(from = min(datos.uvi$fecha),
             to = max(datos.uvi$fecha),
             by = "5 min")

new.y <- predict(fit, newdata = data.frame(fecha = as.numeric(new.x)))


DF <- data.frame(x1 = head(new.x, -1), x2 = tail(new.x, -1) , 
                 y1 = head(new.y, -1), y2 = tail(new.y, -1))
DF$col <- cut(DF$y1, c(-Inf, 250, 500, Inf))


ggplot(data=DF, aes(x=x1, y=y1, xend = x2, yend = y2, colour=col)) +
  geom_segment(size = 2)

注意切点处发生的情况。如果将 x-grid 用于预测可能更具视觉吸引力,然后使用 geom_point 代替。但是,绘图会很慢。

【讨论】:

  • 感谢@Roland 非常接近我的需要,调整 x-grid 并减小段大小运行良好
【解决方案2】:

这并不是您真正要求的,但可能具有相同的目的:不是为线条着色,而是为背景着色。首先我们创建一个矩形/限制坐标的数据框。

rect_data <- data.frame(xmin=min(datos.uvi$fecha),
                          xmax=max(datos.uvi$fecha),
                          ymin=c(0,250,500),
                          ymax=c(250,500,max(datos.uvi$Rad_Global_.mW.m2.)),
                          col=c("red","green","blue"))

然后我们使用 scale_fill_identity() 将它们添加到绘图中

ggplot(data=datos.uvi) +
  geom_smooth(aes(x=fecha, y=Rad_Global_.mW.m2.),colour="black",se=FALSE, span=0.3) +
  geom_rect(data=rect_data, aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,fill=col),alpha=0.1)+
  scale_fill_identity()

【讨论】:

  • 嗨@heroka,虽然不完全是我需要的一种非常好的方法,它可以提供相同的结果。我接受了罗兰的回答,但你的回答值得被选中。谢谢
  • 不用担心,Rolands 的回答更好,并且回答了实际问题。这更像是“这是解决较大问题而不是解决较小问题的另一种方法”-答案。
  • 我建议使用c(-Inf, Inf) 作为矩形的限制,以覆盖整个绘图区域
猜你喜欢
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多