R 在另一个图形的角上绘制图形

R 在另一个图形的角上绘制图形,r,graphics,plot,R,Graphics,Plot,我应该如何在R中的另一个绘图的角落中显示一个小绘图?我使用了以下方法: # Making some fake data plot1 <- data.frame(x=sample(x=1:10,10,replace=FALSE), y=sample(x=1:10,10,replace=FALSE)) plot2 <- data.frame(x=sample(x=1:10,10,replace=FALSE),

我应该如何在R中的另一个绘图的角落中显示一个小绘图?

我使用了以下方法:

# Making some fake data
plot1 <- data.frame(x=sample(x=1:10,10,replace=FALSE),
                    y=sample(x=1:10,10,replace=FALSE))
plot2 <- data.frame(x=sample(x=1:10,10,replace=FALSE),
                    y=sample(x=1:10,10,replace=FALSE))
plot3 <- data.frame(x=sample(x=1:10,10,replace=FALSE),
                    y=sample(x=1:10,10,replace=FALSE))

layout(matrix(c(2,1,1,3,1,1),2,3,byrow=TRUE))
plot(plot1$x,plot1$y)
plot(plot2$x,plot2$y)
plot(plot3$x,plot3$y)
     [,1] [,2] [,3]
[1,]    2    1    1
[2,]    3    1    1
所以,你可以这样结束:

# Making some fake data
plot1 <- data.frame(x=sample(x=1:10,10,replace=FALSE),
                    y=sample(x=1:10,10,replace=FALSE))
plot2 <- data.frame(x=sample(x=1:10,10,replace=FALSE),
                    y=sample(x=1:10,10,replace=FALSE))
plot3 <- data.frame(x=sample(x=1:10,10,replace=FALSE),
                    y=sample(x=1:10,10,replace=FALSE))

layout(matrix(c(2,1,1,3,1,1),2,3,byrow=TRUE))
plot(plot1$x,plot1$y)
plot(plot2$x,plot2$y)
plot(plot3$x,plot3$y)
     [,1] [,2] [,3]
[1,]    2    1    1
[2,]    3    1    1

编辑以添加:

好的,如果你想在角落里整合一个绘图,你可以使用相同的
layout
命令,只需改变矩阵。例如,这是不同的代码:

layout(matrix(c(1,1,2,1,1,1),2,3,byrow=TRUE))
plot1 <- data.frame(x=1:10,y=c(9,10,8,7,3,4,1,2,5,6))
plot2 <- data.frame(x=1:10,y=c(6,7,5,1,2,8,3,10,9,4))
plot(plot1$x,plot1$y,type="o",col="red")
plot(plot2$x,plot2$y,type="o",xlab="",ylab="",main="",sub="",col="blue")
出来的情节是这样的:

# Making some fake data
plot1 <- data.frame(x=sample(x=1:10,10,replace=FALSE),
                    y=sample(x=1:10,10,replace=FALSE))
plot2 <- data.frame(x=sample(x=1:10,10,replace=FALSE),
                    y=sample(x=1:10,10,replace=FALSE))
plot3 <- data.frame(x=sample(x=1:10,10,replace=FALSE),
                    y=sample(x=1:10,10,replace=FALSE))

layout(matrix(c(2,1,1,3,1,1),2,3,byrow=TRUE))
plot(plot1$x,plot1$y)
plot(plot2$x,plot2$y)
plot(plot3$x,plot3$y)
     [,1] [,2] [,3]
[1,]    2    1    1
[2,]    3    1    1

我知道这个问题已经解决了,但我要为子孙后代举个例子

一旦掌握了基本知识,您就可以很容易地使用基本的“网格”包进行自定义可视化。下面是我使用的一些自定义函数的一个快速示例,以及绘图数据的演示


自定义功能


您还可以使用
par(图=…,新=真)


xTeachingDemos包中的
子绘图
函数正是针对基本图形执行此操作的


创建全尺寸绘图,然后使用子绘图中所需的绘图命令调用
子绘图
,并指定子绘图的位置。可以通过关键字(如“topleft”)指定位置,或者您可以为其提供当前绘图用户坐标系中的坐标。

@TAReham谢谢,如果我想在另一个绘图中创建一个小绘图?@TAReham for axample一个小绘图就在另一个绘图的顶部和内部?@TAReham再次感谢您。相比之下,这无疑让我的回答显得相当软弱@不过,TARehman的主要区别在于,答案中的
par
方法可以接受基本绘图函数,而“网格”方法将要求您指定自己的绘图方法。这主要是一个时间问题,以及你的情节需要如何定制。这个问题并没有结束,OP绝对有能力改变他们的选择,哪一个答案得到了选中标记。使用ggplot2,参见
?annotation\u custom
中的最后一个示例,我非常喜欢这个选项,因为它简单且能够使用基本绘图功能。作为一个“网格”用户,我自己没有使用它的能力,但我必须记住这一点,以供其他人询问。谢谢你指出这一点。