R 用多图绘制

R 用多图绘制,r,plot,R,Plot,我想生成如下图: 这是一个演示称为小波的函数的“拨号”和“转换”的图。这里的函数并不重要,只是我如何生成这样的图 谁能给我一个提示,哪种方法最好?我怎样才能得到绘图中的六个坐标轴?我应该从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

我想生成如下图:

这是一个演示称为小波的函数的“拨号”和“转换”的图。这里的函数并不重要,只是我如何生成这样的图

谁能给我一个提示,哪种方法最好?我怎样才能得到绘图中的六个坐标轴?我应该从
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)

mo我将从以下内容开始:

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)

这里有一个解决方案,它使用
布局
创建一个特定的绘图布局,在左侧和顶部为长箭头添加额外的面板

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

“我可能自己做,但需要太多时间,而我现在没有时间”这意味着代码编写服务也是如此,但事实并非如此。您可能需要重新表述。请查看
par
中的
fig
选项。它允许你在区域的某个特定百分比上绘图。在这种情况下,您可能希望在绘图区域的底部80%(给予或接受)使用
mfrow=c(3,3)
。我肯定会在
ggplot2
中查看
facet\u wrap
facet\u grid
。这些多方面的绘图处理了大量的工作。我从来没有使用过包
ggplot2
,所以它会花费太多的时间…你的结果怎么了?如果边距太宽,请调整
par(mar=c(…)
。谢谢你的建议!看起来不错,但问题是,在你的版本中,绘图看起来像是分开的,但这不是我想要的印象。小波应用于相同的时间序列,在“转换”和“放大”中版本,以便生成二维分解。@r学生抱歉,我不明白你的意思。是否有指向已知解决方案的链接?在我看来,你需要一种混合方法,并进行一些艺术编辑。看起来不错!也许你有一个建议,如何像我原来的箭头一样将文本添加到整体箭头中?使用
多行文字
,添加到m中邮递员。
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)
}