Question

I'd like plot this :

enter image description here

And I have this data set:

datetime,temperature,humidity,dpv
"2014-02-15 00:00:00",67.2,13.6,"red"
"2014-02-15 00:15:00",63.4,13.8,"yellow"
"2014-02-15 00:30:00",61.2,14.2,"green"
"2014-02-15 00:45:00",60.4,14.5,"green"

Where dpv is the value for the color at bottom.

Need mix scales and put humidity at right side axis.

This is my code getting data:

datos_tem$datetime <- as.POSIXct(datos_tem$datetime)
datos_tem$temperature <- as.numeric(datos_tem$temperature)
datos_tem$humidity <- as.numeric(datos_tem$humidity)

but I don't know do it with PLOT (not ggplot2) library.

Thanks in advance.

Was it helpful?

Solution

Not exactly the same but pretty similar:
enter image description here

Code:

datos_tem <- 
  read.csv(text=
             'datetime,temperature,humidity,dpv
           "2014-02-15 00:00:00",67.2,13.6,"red"
           "2014-02-15 00:15:00",63.4,13.8,"yellow"
           "2014-02-15 00:30:00",61.2,14.2,"green"
           "2014-02-15 00:45:00",60.4,14.5,"green"')

datos_tem$datetime <- as.POSIXct(datos_tem$datetime)
datos_tem$temperature <- as.numeric(datos_tem$temperature)
datos_tem$humidity <- as.numeric(datos_tem$humidity)
datos_tem$dpv <- as.character(datos_tem$dpv)

par(mar=c(5,4,4,5)+0.1)
# add temperature
plot(datos_tem$datetime,datos_tem$temperature,typ='b',col='red',
     ylab='Temperature',xlab='Time')

# add humidity
par(new=T)
plot(datos_tem$datetime,datos_tem$humidity,type='b', 
     col='blue',xaxt='n',yaxt='n',xlab='',ylab='')
axis(4)
mtext(side=4,text='Humidity',line=3)

# add colored bars
par(new=T)

xToler <- min(diff(datos_tem$datetime)) / 2
yToler <- (min(datos_tem$humidity) - par('usr')[3]) / 2

enc <- rle(datos_tem$dpv)

idx <- 1
for(i in 1:length(enc$lengths)){
  start <- datos_tem$datetime[idx] - xToler
  end <- datos_tem$datetime[idx + enc$lengths[i] - 1] + xToler
  rect(xleft=start,xright=end,
       ybottom=par('usr')[3],ytop=par('usr')[3]+yToler,col=enc$values[i])
  idx <- idx + enc$lengths[i]
}

Note:

The code is pretty generic, i.e. it automatically computes positions according to the data in the original "datos_tem" data.frame.

OTHER TIPS

Plots like that are recommended against. In the case of your example plot the eye is drawn to the point where the 2 lines cross, but this point is due to arbitrary scale choice rather than anything meaningful. Consider the change in the plot if you changed the temperature range to only go to 14 instead of 20, then the crossing point would be in a different location. This plot draws the eye to a meaningless feature rather than emphasizing differences of interest.

Better would be to use layout or par(mfrow=c(2,1)) to stack 2 plots on top of each other so the mutual increasing can still be seen, but there is no crossing point and which scale goes with which data is completely clear.

If you still insist on the inferior plot then look at twoord.plot in the plotrix package or updateusr in the TeachingDemos package (in conjunction with plot and lines).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top