-2

I am quite new in R.

I am doing a part of my MSc thesis and wanna make some diurnal plots of for instance methane production in a period of time.

Now I a wanna see its variation in time and its correlation with another factor in the same time. Then I have two questions.

First:

How to define the xlim and ylim to increase by 2 hours. It has its own default and when I give it for example:

xlim = c(0, 23)

then it starts from 0 and goes up in 5 hours. I want it to go up in 2 hours.

Second:

How to put another variable which might be correlated to my first variable in the same time period. Let's say methane production in 23 hours could be related to oxygen consumption, just as an example. How can I put oxygen and methane in the same axis(y) against time (x)?

I will be so appreciated if you could help me with this.

Kinds, Farhad

Artem
  • 3,304
  • 3
  • 18
  • 41
  • 2
    @farhad-m-panah welcome to SO. See here on how to create a [minimum reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and do ask 1 question per post only. – mnm Aug 09 '18 at 13:52

1 Answers1

0
  1. You can use at and labels arguments in axis function call to customize labels and tick locations.
  2. You can use axis function with argument side = 4 to create custom y-axis on the right of you graph.

Please see the code below illustrating the above mentioned points:

set.seed(123)

x <- 0:23

df<- data.frame(
  x,
  ch4 = 1000 - x ^ 2,
  o2 =  2000 - 2 * (x - 10) ^ 2
)

par(mar = c(5, 5, 2, 5))
with(df, plot(x, ch4, 
              type = "l", col = "red3", 
              ylab = "CH4 emission",
              lwd = 3,
              xlim = c(0, 23),
              xlab = "",
              xaxt = "n"))
axis(1, at = seq(0, 23, 2), labels = seq(0, 23, 2))

par(new = TRUE)
with(df, plot(x, o2, 
              pch = 16, axes = FALSE, 
              xlab = NA, ylab = NA, cex = 1.2))
axis(side = 4)

mtext(side = 4, line = 3, "O2 consumption")

legend("topright",
       legend = c("O2", "CH4"),
       lty = c(1, 0), 
       lwd = c(3, NA),
       pch = c(NA, 16), 
       col = c("red3", "black"))

Output:

diurinal plots

Artem
  • 3,304
  • 3
  • 18
  • 41