【问题标题】:How to plot (almost) the same function at both sides of the "y" axis in R?如何在 R 中的“y”轴两侧绘制(几乎)相同的函数?
【发布时间】:2015-05-02 03:29:25
【问题描述】:

我有一个函数,它取决于距离,并且根据您评估它的方向(东或西)而表现不同。现在我有两个并排的图,但我需要将它们作为一个图,其中轴的标签是共享的,x 轴的标签是居中的。下面是代码现在看起来的示例:

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))

如您所见,绘图的对数刻度不允许评估“负”值,因此我无法仅使用一个绘图。

我如何在不使用 Illustrator 或其他图形软件的情况下将此图作为一个图,因为我必须为不同的区域创建很多这样的图?

我过去没有使用过ggplot,但如果有必要我愿意学习。

【问题讨论】:

    标签: r plot


    【解决方案1】:

    您基本上可以制作一个情节并与fig 混在一起,以便将第一个情节限制在设备的左半边,将第二个情节限制在右半边

    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)
    

    祝你好运。你可能不得不召唤@baptiste

    我想你仍然可以使用mfrow

    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)
    

    【讨论】:

    • @AlexA。我怀疑哈德利会同意这个 x 轴
    • 感谢您的回答,这正是我想做的,它给了我一些线索来解决我遇到的其他问题。
    猜你喜欢
    • 2014-09-08
    • 1970-01-01
    • 2016-09-28
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多