简体   繁体   中英

How to plot (almost) the same function at both sides of the “y” axis in R?

I have a function that depends on distance and behaves different according to the direction (east or west) to where you evaluate it. Now I have two plots side by side, but I need to have them as one, where the labels of the axis are shared and the x axis label is centered. Here is an example of how the code looks 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 of the plots don't allow "negative" values to be evaluated so I haven't been able to use only one plot.

How can I get this plot as one without using Illustrator or another graphics software, as I have to create lots of this ones for differente areas?

I haven't used ggplot in the past but I am willing to learn if necessary.

You can basically make one plot and mess around with fig so that you restrict 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)

在此输入图像描述

good luck with ggplot. you'd probably have to summon @baptiste

well you could 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM