0

I have 2 csv data files. Each file has a "date_time" column and a "temp_c" column. I want to make the x-axis have the "date_time" from both files and then use 2 y-axes to display each "temp_c" with separate lines. I would like to use plot instead of ggplot2 if possible. I haven't been able to find any code help that works with my data and I'm not sure where to really begin. I know how to do 2 separate plots for these 2 datasets, just not combine them into one graph.

plot(grewl$temp_c ~ grewl$date_time) 

and

plot(kbll$temp_c ~ kbll$date_time) 

work separately but not together.

neilfws
  • 32,751
  • 5
  • 50
  • 63
  • 1
    Look at `?points` or `?lines` for the base R way to add more data to an existing plot. – neilfws Aug 26 '19 at 23:04
  • You should give a small sample of your two data sets so that we can help you. Please read [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – G5W Aug 26 '19 at 23:13
  • Sorry. I'm very new at using R. will this help? > head(grewl) grewingk_2 date_time temp_c humidity_rh dew_point file_name 1 1 3/20/19 20:24 15.5 54.5 6.4 grewingk_2_050719 2 2 3/20/19 20:39 17.5 40.5 3.9 grewingk_2_050719 3 3 3/20/19 20:54 16.0 40.5 2.6 grewingk_2_050719 4 4 3/20/19 21:09 15.5 41.0 2.3 grewingk_2_050719 5 5 3/20/19 21:24 15.0 41.5 2.0 grewingk_2_050719 6 6 3/20/19 21:39 15.0 42.0 2.2 grewingk_2_050719 – sydney3989 Aug 26 '19 at 23:38
  • > head(kbll) KasBayLab date_time temp_c humidity_rh dew_point file_name 1 1 3/22/19 20:51 14.0 47.0 2.9 KasBayLab_050319.csv 2 2 3/22/19 21:06 7.0 67.0 1.3 KasBayLab_050319.csv 3 3 3/22/19 21:21 4.0 81.0 1.0 KasBayLab_050319.csv 4 4 3/22/19 21:36 3.5 86.0 1.4 KasBayLab_050319.csv 5 5 3/22/19 21:51 3.5 87.5 1.6 KasBayLab_050319.csv 6 6 3/22/19 22:06 3.0 88.0 1.2 KasBayLab_050319.csv > – sydney3989 Aug 26 '19 at 23:39
  • 1
    @sydney3989: please do not use `head()`. Make sure that you read the link G5W shared – Tung Aug 26 '19 at 23:55

1 Answers1

0

As others indicated, it is easy to add new data to a graph using points() or lines(). One thing to be careful about is how you format the axes as they will not be automatically adjusted to fit any new data you input using points() and the like.

I've included a small example below that you can copy, paste, run, and examine. Pay attention to why the first plot fails to produce what you want (axes are bad). Also note how I set this example up generally - by making fake data that showcase the same "problem" you are having. Doing this is often a better strategy than simply pasting in your data since it forces you to think about the core component of the problem you are facing.

#for same result each time
set.seed(1234)
#make data
set1<-data.frame("date1" = seq(1,10),
                 "temp1" = rnorm(10))
set2<-data.frame("date2" = seq(8,17),
                 "temp2" = rnorm(10, 1, 1))

#first attempt fails
#plot one
plot(set1$date1, set1$temp1, type = "b")
#add points - oops only three showed up bc the axes are all wrong
lines(set2$date2, set2$temp2, type = "b")

#second attempt
#adjust axes to fit everything (set to min and max of either dataset)
plot(set1$date1, set1$temp1,
     xlim = c(min(set1$date1,set2$date2),max(set1$date1,set2$date2)),
     ylim = c(min(set1$temp1,set2$temp2),max(set1$temp1,set2$temp2)),
     type = "b")
#now add the other points
lines(set2$date2, set2$temp2, type = "b")

# we can even add regression lines
abline(reg = lm(set1$temp1 ~ set1$date1))
abline(reg = lm(set2$temp2 ~ set2$date2))
arwood
  • 136
  • 1
  • 1
  • 5