【问题标题】:Plot with multiple graphs绘制多个图
【发布时间】:2014-04-22 07:23:36
【问题描述】:

我想生成如下图:

这是一个演示小波函数的“拨号”和“平移”的图。这里的函数并不重要,重要的是如何生成这样的图。

谁能给我一个提示,这是最好的方法吗?如何获得图中的六个坐标轴?我应该以par(mfrow=c(3,3)) 开头吗?

到目前为止,我已经使用以下代码进行了管理:

mo<-function(t,trans=0,omega=6,j=0){
  dial<-2*2^(j*.125)
  sqrt((1/dial))*pi^(-1/4)*exp(1i*omega*((t-trans)/dial))*exp(-((t-trans)/dial)^2/2)
}

par(mfrow=c(3,3))

plot(seq(-10,10,length=1000),Re(mo(seq(-10,10,length=1000),trans=-3)),axes=F,xlab="",ylab="",type="l")
abline(h=0,v=0)

plot(seq(-10,10,length=1000),Re(mo(seq(-10,10,length=1000),trans=0)),axes=F,xlab="",ylab="",type="l")
abline(h=0,v=0)

plot(seq(-10,10,length=1000),Re(mo(seq(-10,10,length=1000),trans=3)),axes=F,xlab="",ylab="",type="l")
abline(h=0,v=0)


#############

plot(seq(-10,10,length=1000),Re(mo(seq(-10,10,length=1000),trans=-3,j=6)),axes=F,xlab="",ylab="",type="l")
abline(h=0,v=0)

plot(seq(-10,10,length=1000),Re(mo(seq(-10,10,length=1000),trans=0,j=6)),axes=F,xlab="",ylab="",type="l")
abline(h=0,v=0)

plot(seq(-10,10,length=1000),Re(mo(seq(-10,10,length=1000),trans=3,j=6)),axes=F,xlab="",ylab="",type="l")
abline(h=0,v=0)


#############

plot(seq(-10,10,length=1000),Re(mo(seq(-10,10,length=1000),trans=-3,j=6)),axes=F,xlab="",ylab="",type="l")
abline(h=0,v=0)

plot(seq(-10,10,length=1000),Re(mo(seq(-10,10,length=1000),trans=0,j=6)),axes=F,xlab="",ylab="",type="l")
abline(h=0,v=0)

plot(seq(-10,10,length=1000),Re(mo(seq(-10,10,length=1000),trans=3,j=6)),axes=F,xlab="",ylab="",type="l")
abline(h=0,v=0)

但这仍然不是我需要的

【问题讨论】:

  • “我自己可以做,但需要太多时间,我现在没有”暗示 SO 是一个代码编写服务,但它不是。你可能想改写一下。
  • 查看par 中的fig 选项。它允许您在该区域的某个百分比上进行绘图。在这种情况下,您可能希望在绘图区域的底部 80%(给予或接受)上使用 mfrow = c(3, 3)
  • 我肯定会看看ggplot2 中的facet_wrapfacet_grid。这些处理了这类多面图的大量工作。
  • 我从来没有使用过ggplot2这个包,所以它会花费太多时间......
  • 您的结果有什么问题?如果边距太宽,调整par(mar=c(...)

标签: r plot


【解决方案1】:

这是一个使用layout 来创建特定的绘图排列的解决方案,左侧和顶部的长箭头有额外的面板

par(mar=c(1,1,1,1), oma=c(5,4,4,2))
m = matrix( c(0, 1, 1, 1,
              2, 3, 4, 5,
              2, 6, 7, 8, 
              2, 9, 10, 11),
        byrow=T, ncol=4)
l = layout(mat=m, widths=c(.1, .2, .2, .2), heights= c(.1, .2, .2, .2))

# check if the layout is as we expect
layout.show(l)

# add top arrow to panel 1
plot(1, xlim=c(1,10), ylim=c(0,1), type='n', axes=F, ann=F)
arrows(x0=1, x1=10, y0=0.5, y1=0.5)
mtext(side=3,'some text')
# add left arrow to panel 2
plot(1, xlim=c(0,1), ylim=c(1,10), type='n', axes=F, ann=F)
arrows(y0=10, y1=1, x0=0.5, x1=0.5)
mtext(side=2,'some text')

# add plots to panels 3:11
for (i in 1:9){

    curve(sin(x), -2*pi, 2*pi, axes=F, ann=F)

    # get x and y extents of plot for drawing arrows
    ymax = par('usr')[4]
    ymin = par('usr')[3]
    xmax = par('usr')[2]
    xmin = par('usr')[1]
    arrows(x0=0, y0=ymin, x1=0, y1=ymax, length=0.1)
    arrows(x0=xmin, y0=0, x1=xmax, y1=0, length=0.1)
}

【讨论】:

  • 看起来不错!也许你有一个建议如何像我原来的那样向整体箭头添加文本?​​
  • 使用mtext,添加到我的帖子中。
【解决方案2】:

我会从这样的开始:

fmo<-function(t,trans=0,omega=6,j=0){
  dial<-2*2^(j*.125)
  sqrt((1/dial))*pi^(-1/4)*exp(1i*omega*((t-trans)/dial))*exp(-((t-trans)/dial)^2/2)
}

dat <- expand.grid(x=seq(-10,10, length = 200),
               trans = c(-3,0,3), 
               j = c(0, 6, 12))
dat$g <- rep(1:9, each = 200)
dat$y <- with(dat, Re(fmo(t=x,trans=trans,omega=6,j=j)))

library(ggplot2)
ggplot(dat) + geom_line(aes(x=x, y=y)) + 
  facet_wrap(~g, ncol = 3)

并从这里开始使用一些主题格式

ggplot(dat) + geom_line(aes(x=x, y=y)) + 
  geom_vline(aes(xintercept=0)) +
  facet_wrap(~g, ncol = 3) +
  theme_classic()

【讨论】:

  • 感谢您的建议!看起来不错,但问题是,在您的版本中,情节看起来像是分开的,但这不是我想要给人的印象。小波以“平移”和“膨胀”版本应用于相同的时间序列,以生成二维分解。
  • @RStudent 对不起,我不明白你的意思。任何指向已知解决方案的链接?在我看来,您需要一种混合方法,并进行一些艺术编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 2020-07-22
  • 2019-12-22
  • 1970-01-01
  • 2014-03-06
  • 2021-09-08
相关资源
最近更新 更多