-2

I'm trying to create a plot with two Y-axis, each one coming from a different data frame. Is this possible?

I have created the two plots separately (temp_EXERCISEonly and DP_EXERCISEonly). Part of my script looks like this:

dftestA = dftest[dftest$orientation == 'East interior stud']

temp_EXERCISEonly <- ggplot(dftestA, aes(x=read_date)) +
  #geom_point() +
  geom_line(aes(y=Temperature), color='red',size = .5, alpha = 0.7) +
  xlab("Time") +
  ylab("Temperature (°F)") +
  scale_x_datetime(
    limits = c(
      as.POSIXct("2016-08-01 00:00:00 "),
      as.POSIXct("2019-07-31 23:00:00 ")),
    breaks = date_breaks("4 months"),
    labels = date_format("%m/%Y")) +
  theme_bw()

temp_EXERCISEonly

#Trying to redo the plot to show condensation risks 
#B Exterior Condition (Scenario of concern: summer)
#Dewpoint

dftestA = dftest[dftest$orientation == 'B Exterior Condition']

DP_EXERCISEonly <- ggplot(dftestA, aes(x=read_date)) +
  #geom_point() +
  geom_line(aes(y=Dewpoint), color='blue',size = .5, alpha = 0.7) +
  xlab("Time") +
  ylab("Dewpoint (°F)") +
  scale_x_datetime(
    limits = c(
      as.POSIXct("2016-08-01 00:00:00 "),
      as.POSIXct("2019-07-31 23:00:00 ")),
    breaks = date_breaks("4 months"),
    labels = date_format("%m/%Y")) +
  theme_bw()

DP_EXERCISEonly

Also, this is a small sample of my data:

dataframe

This is also how the column dftest$orientation looks like:

[1] "A Interior Condition"     "B Exterior Condition"     "East interior stud"      
   [4] "East exterior sheathing"  "South interior stud"      "South exterior sheathing"
   [7] "West interior stud"       "West exterior sheathing"  "North interior stud"     
  [10] "North exterior sheathing" ```

I hope someone can help, any guidance would be much appreciated. Thank you.

Adam Quek
  • 5,930
  • 1
  • 13
  • 23
  • Hi! Please add a small sample of your data so you have a better chance of finding an answer. You can use, for example, `dput(head(dftestA, 20))` among other methods. – Juan Bosco May 27 '22 at 21:26
  • Thank you, Juan. I am doing my best, I'm an international student, new to the programming language. – Diana B. Picciotto May 27 '22 at 21:43
  • Hi @DianaB.Picciotto. Welcome to StackOverflow. Unfortunately, we can't help you if we do not have a minimum dataset that can reproduce your problem. Please refer to this thread on [how to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) before asking your question. Thanks! – Adam Quek May 28 '22 at 11:20
  • easiest way is to merge your data first (joining by your x variable), e.g. with `dplyr::left_join(df1, df2, by = "read_date")`, and then plot two y axis with https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales – tjebo May 28 '22 at 11:49

0 Answers0