简体   繁体   English

如何绘制镜像图? (两个图共享相同的x轴,但一个图上下颠倒-在该轴下方)

[英]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: 给出这个情节:

1 1个

This graph is present in the R graph gallery 该图存在于R图库中

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

相关问题 两个图相同的x轴,两个y轴-如何在网格中排列图 - Two plots same x axis, two y axis - how to arrange plots in a grid 合并两个不同的图:一个在X轴上,另一个在Y轴上 - Merge two different plots: one in the X-axis and the other in the Y-axis x轴标签在某些图上未显示(堆叠的条形图) - x-axis labels are not showing at some plots (stacked bar plot) 将ggplot图设置为在点图行之间具有相同的x轴宽度和相同的空间 - set ggplot plots to have same x-axis width and same space between dot plot rows 两个具有相同X和Y轴的图 - Two plots with same X and Y axis 在 r 中,如何控制组合图 (plot_grid) 中图与 x 轴的比例? - In r, how to control proportion of plots versus x-axis in a combined plot (plot_grid)? 如何将 x 轴标签添加到时间序列图中? - How to add x-axis labels to time series plots? R基图:在1个坐标图的x轴和y轴上绘制2个密度图 - R baseplot :plotting 2 density plots in 1 plot cand hanging scales for x-Axis and y-Axis 如何在 R 中手动创建箱线图,x 轴上有两个类别 - How to manually create box plots in R with two categories on x-axis 对偶绘图在ggplot2中共享相同的x轴 - Dual plot sharing the same x-axis in ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM