How do I plot the (almost) the same function on both sides of the "y" axis in R?

I have a function that is distance dependent and behaves differently depending on the direction (east or west) to where you evaluate it. Now I have two plots side by side, but I need them to be as a single unit, where the axis labels are separated and the x axis label is centered. Here's an example of what the code looks like right now:

x = (0:300)
par(mfrow=c(2,2))

Idriss70 = function(x){
exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(x+10)+0.00047*x+0.12)
}
plot(Idriss70(x), log = "x", type="l",xlim = c(300,1), xlab="Distancia [km]",ylab="PGA [g]", main="Aroma y Humayani extendida, Mw 7,0", col="green",   panel.first = grid(equilogs = TRUE))

Idriss70 = function(x){
ifelse (x >= 0 & x<=3, exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(0+10)+0.00047*0+0.12),
      exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(x+10)+0.00047*x+0.12))
}
plot(Idriss70(x), log = "x", type="l", xlab="Distancia [km]",ylab="PGA [g]", main="Aroma y Humayani extendida, Mw 7,0", col="green", panel.first = grid(equilogs = TRUE))

      

As you can see, the log scale on the graphs does not allow evaluating "negative" values, so I was unable to use just one graph.

How can I get this plot as one without using Illustrator or other graphics software, since I need to create a lot of this for different areas?

I have not used ggplot in the past, but I am willing to learn if needed.

+3


source to share


1 answer


Basically you can create one plot and combine with fig

so that you limit the first plot to the left half of the device and the second to the right half.

x <- 0:300

Idriss70 = function(x){
  exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(x+10)+0.00047*x+0.12)
}

Idriss71 = function(x){
  ifelse(x >= 0 & x<=3,
         exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(0+10)+0.00047*0+0.12),
         exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(x+10)+0.00047*x+0.12))
}

par(fig = c(0, .5, 0, 1), mar = c(5, 4, 4, 0), xaxs = 'i', ann = FALSE)
plot(Idriss70(x), log = "x", type="l", xlim = c(300,1),
     col="green", axes = FALSE, panel.first = grid(equilogs = TRUE))
xx <- axis(1)
axis(2)

par(fig = c(.5, 1, 0, 1), new = TRUE, mar = c(5, 0, 4, 2))
plot(Idriss71(x), log = "x", type="l", col="green",
     panel.first = grid(equilogs = TRUE), axes = FALSE)
axis(1, at = xx, labels = c('', xx[-1]))

title(xlab = "Distancia [km]", main = 'Aroma y Humayani extendida, Mw 7,0',
      ylab = 'PGA [g]', outer = TRUE, line = -1.5)

      

enter image description here



good luck with ggplot. you probably have to call @baptiste

well you can still use mfrow

I suppose

graphics.off()
par(mfrow = c(1, 2), mar = c(5, 4, 4, 0), xaxs = 'i', ann = FALSE)
plot(Idriss70(x), log = "x", type="l", xlim = c(300,1),
     col="green", axes = FALSE, panel.first = grid(equilogs = TRUE))
xx <- axis(1)
axis(2)

par(mar = c(5, 0, 4, 2))
plot(Idriss71(x), log = "x", type="l", col="green",
     panel.first = grid(equilogs = TRUE), axes = FALSE)
axis(1, at = xx, labels = c('', xx[-1]))

title(xlab = "Distancia [km]", main = 'Aroma y Humayani extendida, Mw 7,0',
      ylab = 'PGA [g]', outer = TRUE, line = -1.5)

      

+3


source







All Articles