0

I am MSc student studying honeybees and new to R. I have data of 2 temperature and humidity sensors from inside a beehive and would like to plot them on the same graph with two y scales, one for temperature and one for relative humidity.

Data:

            time  DHT22_t DHT11_t DHT22_h DHT11_h
1 10/5/2014 12:52    15.7      17    77.0      83
2 10/5/2014 13:52    16.7      18    71.6      81
3 10/5/2014 14:51    15.7      18    79.0      81
4 10/5/2014 15:51    15.8      18    77.0      81
5 10/5/2014 16:51    16.0      18    77.0      81
6 10/5/2014 17:50    14.3      17    80.2      81

I used melt to rearrange data w/ respect to time:

meltdf <- melt(Hive1_4days,id="time")

              time variable value
1 10/5/2014 12:52  DHT22_t  15.7
2 10/5/2014 13:52  DHT22_t  16.7
3 10/5/2014 14:51  DHT22_t  15.7
4 10/5/2014 15:51  DHT22_t  15.8
5 10/5/2014 16:51  DHT22_t  16.0
6 10/5/2014 17:50  DHT22_t  14.3

using ggplot:

ggplot(meltdf,aes(x=time,y=value,colour=variable,group=variable)) + geom_line()

enter image description here

Instead of this, I would like to have a humidity scale on the right. Also, The x-axis is saturated with dates (the sensors logged data ~18 times per minute) and I would like to change the scale to every few hours or half day or something. Any ideas? Thanks!

EDIT:

pizza<- ggplot(meltdf,aes(x=time,y=value,colour=variable,group=variable)) + geom_line()
pizza+scale_x_date(breaks=date_breaks("1 days"))

produces this:

enter image description here

Which does not help. Is there a way to change the axis w/o affecting the plot?

Undo
  • 25,519
  • 37
  • 106
  • 129
Evan
  • 217
  • 2
  • 13
  • 1
    See the answer given by @hadley (the author of the `ggplot2` package) in [this question](http://stackoverflow.com/questions/3099219/how-to-use-ggplot2-make-plot-with-2-y-axes-one-y-axis-on-the-left-and-another). – nrussell Feb 02 '15 at 17:29
  • For the date ticks/labels: `library(scales); library(ggplot2); help('date_breaks'); help('scale_x_date')` ; there is no help for your dual-scale issue. Base graphics plotting can help you with that, tho. – hrbrmstr Feb 02 '15 at 18:08
  • For a second y-axis see http://stackoverflow.com/questions/18989001/how-can-i-put-a-transformed-scale-on-the-right-side-of-a-ggplot2, and http://rpubs.com/kohske/dual_axis_in_ggplot2, and – user20650 Feb 02 '15 at 19:24
  • @hrbrmaster using date_breaks and scale_x_date or scale_x_discrete changes the what is being graphed to only those days. I edited OP. Do you know how to fix that? – Evan Feb 02 '15 at 20:55
  • @user20650 that post was linked to this one now – Evan Feb 02 '15 at 21:51
  • Oh no problem, ya i was dissuaded pretty quickly from the idea too – Evan Feb 02 '15 at 22:07

0 Answers0