如何向“顶部"添加插图(子图). R图? [英] How to add an inset (subplot) to "topright" of an R plot?

查看:99
本文介绍了如何向“顶部"添加插图(子图). 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.

这里是一个例子:

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天全站免登陆