[英]How to plot mirror plots? (Two plots sharing the same x-axis, but one is upside down - below the axis)
Suppose I have two samples that I would like to compare graphically. 假设我有两个样本要进行图形比较。 One way to do it is to lay one over another like below:
一种方法是像下面这样将其放置在另一个之上:
x1 = rnorm(100)
x2 = rnorm(100, mean=2)
plot(density(x1))
lines(density(x2), col="red")
I wonder however if there is a way to plot x2 such that the plot shares the same axis as the plot of x1, except that it is upside down, like the plot below. 但是,我想知道是否有一种方法可以绘制x2,使该图与x1的图具有相同的轴,只是它像上面的图一样上下颠倒。 It would be especially great if there is any method that does not involve downloading additional packages.
如果有不涉及下载其他软件包的任何方法,那就特别好。
Thanks! 谢谢!
If it doesn't matter if the y-axis contains values below zero, you can use this: 如果y轴是否包含零以下的值无关紧要,则可以使用以下方法:
x1 <- rnorm(100)
x2 <- rnorm(100, mean=2)
dens1 <- density(x1)
dens2 <- density(x2)
dens2$y <- dens2$y * -1
plot(dens1,
ylim = range(c(dens1$y, dens2$y)),
xlim = range(c(dens1$x, dens2$x)),
main = "",
xlab = "")
lines(dens2, col = "red")
You can reverse the axis of a plot by using the argument ylim=(...)
(or xlim=(...)
) and specifying the limits in the reverse order. 您可以通过使用参数
ylim=(...)
(或xlim=(...)
)并以相反的顺序指定限制来反转图的轴。
For example: 例如:
layout(matrix(1:2, ncol=1))
par(mai=c(0.5, 1, 0.5, 1))
plot(c(-6, 6), 0:1, type="n", ylim=c(0, 1), xlab="", ylab="")
lines(density(x1), ylim=c(0, 1))
plot(c(-6, 6), 0:1, type="n", ylim=c(1, 0), xlab="", ylab="")
lines(density(x2), col="red", ylim=c(1, 0))
It is possible to get only one x-axis using the mar
argument of the par
function. 使用
par
函数的mar
参数只能获得一个x轴。 The R code would look like this: R代码如下所示:
#Create Data
x1 = rnorm(100)
x2 = rnorm(100, mean=2)
#Make the plot
par(mfrow=c(2,1))
par(mar=c(0,5,3,3))
plot(density(x1) , main="" , xlab="", ylim=c(0,1) , xaxt="n", las=1 , col="slateblue1" , lwd=4 )
par(mar=c(5,5,0,3))
plot(density(x2) , main="" , xlab="Value of my variable", ylim=c(1,0) , las=1 , col="tomato3" , lwd=4)
Giving this plot: 给出这个情节:
This graph is present in the R graph gallery 该图存在于R图库中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.