简体   繁体   English

在R中创建辅助y轴

[英]create a secondary y-axis in R

I had a question regarding creating secondary y-axis in R. Here is an example dataset 我有一个关于在R中创建辅助y轴的问题。这是一个示例数据集

#generate some artifical data

per_cur <- runif(1171, 0.1, 7.62)
obs<-runif(1171,100,1000)

#create a density histogram of per_cur 

par(mfrow=c(2,1))
op <- par(mar = c(5,4,4,4) + 0.5)
hist(per_cur, prob=TRUE, border="white",main=NULL,las=1,cex.axis=0.8,ann = FALSE)
lines(density(per_cur), col="blue",lwd=2)

#add obs with a secondary y-axis

par(new = TRUE)
plot(per_cur,obs, cex=.5, pch=16, col=adjustcolor("black",alpha=0.2), axes = FALSE, ylab="Density")
axis(4,cex.axis=0.5)

It produces a plot which tells me the distribution of per_cur and also shows my the relationship between per_cur and obs through the secondary y-axis. 它产生一个图,告诉我per_cur的分布,并通过辅助y轴显示我在per_curobs之间的关系。 However, when I run the following code with the only difference that I set the limit of primary y-axis using ylim=c(0,0.3) you can see the plot completely changes. 但是,当我运行以下代码,唯一的不同是我使用ylim=c(0,0.3)设置了主要y轴的限制时,您会看到该图完全改变了。 It gives the impression that relationship between the obs and pre_cur is different in both plots (more obs points come under the curve in first plot compared to the second plot). 它给人的印象是,在两个图中, obspre_cur之间的关系是不同的(与第二个图相比,第一个图中的曲线下有更多的obs点)。

op <- par(mar = c(5,4,4,4) + 0.5)
hist(per_cur, prob=TRUE,ylim=c(0,0.3), border="white",main=NULL,las=1,cex.axis=0.8,ann = FALSE)
lines(density(per_cur), col="blue",lwd=2)
par(new = TRUE)
plot(per_cur,obs, cex=.5, pch=16, col=adjustcolor("black",alpha=0.2), axes = FALSE, ylab="Density")
axis(4,cex.axis=0.5)

I wanted to ask is there any way my secondary y-axis also get adjusted as I adjust the primary y-axis limit so that equal number of obs points are under the curve in both plots. 我想问的是,在我调整主要y轴限制时,我的次要y轴是否也有调整的方法,以便在两个图中的曲线下都具有相同数量的obs点。 Hope this is clear. 希望这很清楚。 在此处输入图片说明

This can be accomplished by manipulating the ylim on each plot. 这可以通过在每个图上操纵ylim来实现。 For example: 例如:

#generate some artifical data

per_cur <- runif(1171, 0.1, 7.62)
obs<-runif(1171,100,1000)

#create a density histogram of per_cur 


# use these variables to set the limits on all plots
y1max = max(density(per_cur)$y)
y2max = max(obs)



par(mfrow=c(2,1))
op <- par(mar = c(5,4,4,4) + 0.5)
hist(per_cur, prob=TRUE, ylim = c(0, y1max), border="white",main=NULL,las=1,cex.axis=0.8,ann = FALSE)
lines(density(per_cur), col="blue",lwd=2)

#add obs with a secondary y-axis

par(new = TRUE)
plot(per_cur,obs, cex=.5, ylim = c(0, y2max), pch=16, col=adjustcolor("black",alpha=0.2), axes = FALSE, ylab="Density")
axis(4,cex.axis=0.5)

Then you scale each axis by the same factor: 然后,按相同因子缩放每个轴:

# used to scale the axes
factor <- 2

op <- par(mar = c(5,4,4,4) + 0.5)
hist(per_cur, prob=TRUE, ylim = c(0, y1max * factor), border="white",main=NULL,las=1,cex.axis=0.8,ann = FALSE)
lines(density(per_cur), col="blue",lwd=2)
par(new = TRUE)
plot(per_cur,obs, cex=.5, ylim = c(0, y2max * factor),pch=16, col=adjustcolor("black",alpha=0.2), axes = FALSE, ylab="Density")
axis(4,cex.axis=0.5)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM