绘制在一个情节 [英] Plot inside a plot
问题描述
我有图1
curve(exp(x),从= 1到= 5,lwd = 5 )
曲线(150-exp(x),从= 1到= 5,lwd = 5,col =darkblue,add = T)
在里面我想添加下面的图2
par(mar = c(7,7,1,1))
curve(exp(x),from = 1,to = 5,lwd = 7,xlab =chi,ylab =exp (1,标签= NA,at = c(0,5))
轴(2,标签= NA,at = c) (0,150))
text(1,120,Alpha,adj = c(0,0),cex = 3)
text(3.5,10,Beta,adj = c(0,0 ),cex = 3)
为了获得以下内容:
我会也喜欢使情节2透明,以便如果在情节2后面存在情节1的某些要素,他们仍然会显示(就像蓝线一样)。同样重要的是情节2的较大标签以及标签和剔除标签中没有轴。
这可能吗?请仅仅基于R解决方案(没有ggplot2 /没有格子)
好的,这里是一个例子, 10英寸pdf。 (使用 par(fig =)
等等令人沮丧的部分原因是它们的效果非常依赖于绘图设备的大小。)
编辑添加一些解释: 基本图形绘图参数 par(fig)
描述/设置图形区域的位置作为绘图区域的比例(通常是整个设备,用于单个数字地块)。它需要一个由 0 $之间的数字(比例)组成的
形式的长度为4的向量c $ c>和 1
。
这里我使用 grconvertX()
和 grconvertY()
来转换xy位置,这些位置是以大图自己的(也称为user
)坐标系统到ndc
(标准化设备坐标)坐标系中。 user
坐标系更加人性化,而ndc
表达式)由 par(fig)
使用的坐标系统。 grconvert *()
调用就在那里执行它们之间的转换。
<$ c
curve(exp(x),from = 1,to = 5,lwd = 5)$ b> ## pdf(fig-in-fig.pdf,width = 10,height = 10)
曲线(150-exp(x),从= 1到= 5,lwd = 5,col =darkblue,add = T)
##这是我添加的位。
par(fig = c(grconvertX(c(1,3),from =user,to =ndc),
grconvertY(c(50,125),from =user ,to =ndc)),
mar = c(4,6,1,1),
new = TRUE)
curve(exp(x),from = 1,to = 5,lwd = 7,xlab =chi,ylab =exp(x),cex.lab = 4,axes = F)
axis(1,labels = NA,at = c(0,5))
axis(2,labels = NA,at = c(0,150))
text(1,120,Alpha,adj = c(0,0),cex = 3 )
text(3.5,10,Beta,adj = c(0,0),cex = 3)
## dev.off()
p>
I have the plot 1
curve(exp(x), from=1, to=5, lwd=5)
curve(150-exp(x), from=1, to=5, lwd=5, col="darkblue",add=T)
and inside it i would like to add the following plot 2
par(mar=c(7,7,1,1))
curve(exp(x), from=1, to=5, lwd=7, xlab="chi", ylab="exp(x)", cex.lab=4,axes=F)
axis(1, labels=NA,at=c(0,5))
axis(2, labels=NA,at=c(0,150))
text(1,120,"Alpha",adj=c(0,0),cex=3)
text(3.5,10,"Beta",adj=c(0,0),cex=3)
In order to obtain the following
I would also like to make plot 2 transparent so that if there are some elements of plot 1 behind plot 2 they will still show (just like the blue line). Also important are the bigger labels of plot 2 and the absence of labels and ticks in it's axes.
Is this possible? Please only base R solutions (no ggplot2 / no lattice)
OK, here's an example, which I plotted to a 10 by 10 inch pdf. (Part of what's frustrating about using par(fig = )
et al. is that their effects are very much dependent on the size of the plotting device.)
Edited to add some explanation:
The base graphic plotting parameter par("fig")
describes/sets the location of a figure region as a proportion of the "drawing region" (which is usually the entire device, for single figure plots). It takes a length-4 vector of the form c(xmin, xmax, ymin, ymax)
consisting of numbers (proportions) between 0
and 1
.
Here I use grconvertX()
and grconvertY()
to convert x-y locations expressed in terms of the larger plot's own (a.k.a. "user"
) coordinate system into the "ndc"
(normalized device coordinates) coordinate system. The "user"
coordinate system is more human-user-friendly, while the "ndc"
is (with the caveats expressed above) the coordinate system used by par("fig")
. The grconvert*()
calls are just there to perform the translation between them.
## pdf("fig-in-fig.pdf", width=10, height=10)
curve(exp(x), from=1, to=5, lwd=5)
curve(150-exp(x), from=1, to=5, lwd=5, col="darkblue",add=T)
## Here's the bit I added.
par(fig = c(grconvertX(c(1, 3), from="user", to="ndc"),
grconvertY(c(50, 125), from="user", to="ndc")),
mar = c(4,6,1,1),
new = TRUE)
curve(exp(x), from=1, to=5, lwd=7, xlab="chi", ylab="exp(x)", cex.lab=4,axes=F)
axis(1, labels=NA,at=c(0,5))
axis(2, labels=NA,at=c(0,150))
text(1,120,"Alpha",adj=c(0,0),cex=3)
text(3.5,10,"Beta",adj=c(0,0),cex=3)
## dev.off()
这篇关于绘制在一个情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!