r - Change line color depending on y value with ggplot2 -


i have question line colours in ggplot2. need plot solar radiation data have 6 hourly data, geom_line doest not give "nice" outuput. i've tried geom_smooth , result close need. have new question, possible change line colour depending on y value?

the code used plot is

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) 

in desired output, line should red radiation values under 250, green in 250-500 interval , blue values higher 500. solar radiation plot

is possible geom_smooth? i've tried reuse code here, not find point.

data used plot:

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") 

thanks in advance.

calculate smoothing outside ggplot2 , use geom_segment:

fit <- loess(rad_global_.mw.m2. ~ as.numeric(fecha), data = datos.uvi, span = 0.3) #note warnings  new.x <- seq(from = min(datos.uvi$fecha),              = max(datos.uvi$fecha),              = "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) 

resulting plot

note happens @ cut points. if might more visually appealing make x-grid prediction fine , use geom_point instead. however, plotting slow then.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -