如何将插图(子图)添加到“右上角"R图? [英] How to add an inset (subplot) to "topright" of an R plot?

查看:29
本文介绍了如何将插图(子图)添加到“右上角"R图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在占绘图区域(图形所在区域)宽度和高度的 25% 的绘图中插入一个插图.

I'd like to have an inset within a plot that makes up 25% of the width and height of the plotting area (area where the graphs are).

我试过了:

# datasets
d0 <- data.frame(x = rnorm(150, sd=5), y = rnorm(150, sd=5))
d0_inset <- data.frame(x = rnorm(1500, sd=5), y = rnorm(1500, sd=5))

# ranges
xlim <- range(d0$x)
ylim <- range(d0$y)

# plot
plot(d0)

# add inset
par(fig = c(.75, 1, .75, 1), mar=c(0,0,0,0), new=TRUE)
plot(d0_inset, col=2) # inset bottomright

这会将插图置于绝对右上角,并使用设备宽度的 25%.如何将其更改为图形所在区域的坐标和宽度?

This puts the inset to absolute topright and also uses 25% of the device-width. How can I change it to the coordinates and width of the area where the graphs are?

推荐答案

查看 TeachingDemos 包中的 subplot 函数.它可能会使您尝试做的事情变得更容易.

Look at the subplot function in the TeachingDemos package. It may make what you are trying to do easier.

这是一个例子:

library(TeachingDemos)
d0 <- data.frame(x = rnorm(150, sd=5), y = rnorm(150, sd=5))
d0_inset <- data.frame(x = rnorm(1500, sd=5), y = rnorm(1500, sd=5))

plot(d0)
subplot( 
  plot(d0_inset, col=2, pch='.', mgp=c(1,0.4,0),
    xlab='', ylab='', cex.axis=0.5), 
  x=grconvertX(c(0.75,1), from='npc'),
  y=grconvertY(c(0,0.25), from='npc'),
  type='fig', pars=list( mar=c(1.5,1.5,0,0)+0.1) )

这篇关于如何将插图(子图)添加到“右上角"R图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆