【问题标题】:R double Y axis seperatingR双Y轴分离
【发布时间】:2020-11-19 07:06:36
【问题描述】:

我想知道是否可以将两个图分开(两者都应该在同一个图上,使用双 Y 轴)。所以双重情节应该分成两部分,但实际上并没有将它们分开 - par(mfrow(1,2))

我试图用布局图或latticeExtraggplot 来模仿它,但没有成功。

我有两个不同的数据集,一个用于汇率,一个用于对数回报。

par(mar=c(4,4,3,4))
plot(rates$EURHUF~rates$Date, type="l", ylab="Rate", main="EUR/HUF", xlab="Time")
par(new=TRUE)
plot(reteslog$EURHUF~rateslog$Date, type="l", xaxt="n", yaxt="n", ylab="", xlab="", col="red")
axis(side=4)
mtext("Log return", side=4, line=3)
legend("topleft", c("EUR/HUF Rates","EUR/HUF Logreturns"), col=c("black", "red"), lty=c(1,1))

到目前为止我在这里,我只是不知道如何将它们分开或缩放(可能使用边距或布局?)

非常感谢大家的帮助

【问题讨论】:

    标签: r ggplot2 plot axis


    【解决方案1】:

    我对此有一个不太古怪的解决方案,并且完全在基础上,这很好。要使其正常工作,您只需能够将所有数据强制到相同的规模,这通常并不麻烦。

    这个想法是,一旦您的数据处于相同的比例,您就可以正常绘制所有数据,然后添加显示不同数据各自比例的自定义轴。

    set.seed(1986)
    d01 <- sample(x = 1:20,
                  size = 200,
                  replace = TRUE)
    
    d02 <- sample(x = 31:45,
                  size = 200,
                  replace = TRUE)
    
    # pdf(file = "<some/path/to/image.pdf>",
    #     width = 4L,
    #     height = 4L) # plot to a pdf
    jpeg(file = "<some/path/to/image.jpeg>") # plot to a jpeg
    par(mar=c(3.5, 3.5, 2, 3.5)) # parameters to make things prettier
    par(mgp=c(2.2, 1, 0)) # parameters to make things prettier
    plot(x = 0,
         y = 0,
         type = "n",
         xlim = c(1, 200),
         ylim = c(1, 50),
         xlab = "Label 01!",
         ylab = "Label 02!",
         axes = FALSE,
         frame.plot = TRUE)
    points(d01,
           pch = 1,
           col = "blue") # data 01
    points(d02,
           pch = 2,
           col = "red") # data 02
    mtext("Label 03!",
          side = 4,
          line = 2) # your extra y axis label
    xticks <- seq(from = 0,
                  to = 200,
                  by = 50) # tick mark labels
    xtickpositions <- seq(from = 0,
                          to = 200,
                          by = 50) # tick mark positions on the x axis
    axis(side = 1,
         at = xtickpositions,
         labels = xticks,
         col.axis="black",
         las = 2,
         lwd = 0,
         lwd.ticks = 1,
         tck = -0.025) # add your tick marks
    y01ticks <- seq(from = 0,
                  to = 1,
                  by = 0.1) # tick mark labels
    y01tickpositions <- seq(from = 0,
                            to = 50,
                            by = 5) # tick mark positions on the y01 axis
    axis(side = 2,
         at = y01tickpositions,
         labels = y01ticks,
         las = 2,
         lwd = 0,
         lwd.ticks = 1,
         tck = -0.025) # add your tick marks
    y02ticks <- seq(from = 0,
                   to = 50,
                   by = 5L) # tick mark labels
    y02tickpositions <- seq(from = 0,
                            to = 50,
                            by = 5) # tick mark positions on the y02 axis
    axis(side = 4,
         at = y02tickpositions,
         labels = y02ticks,
         las = 2,
         lwd = 0,
         lwd.ticks = 1,
         tck = -0.025) # add your tick marks
    dev.off() # close plotting device
    

    一些注意事项:

    1. 此绘图的大小最初设置为 pdf,遗憾的是无法在此处上传,但是该设备调用包含在上面注释掉的代码中。您可以随时使用参数来找出最适合您的方法。
    2. 使用 mtext() 绘制所有轴标签会很有优势。
    3. 在原始帖子中包含简单示例数据通常比您使用的确切数据更有帮助。在我撰写本文时,我真的不知道您的数据是什么样的,因为我无权访问这些对象。

    【讨论】:

    • 哇,这很有帮助!非常感谢尼克!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多