如何为 R 中 x 轴镜像的两个变量创建条形图?
我有一个包含 x 变量和两个 y1 和 y2 变量(总共 3 列)的数据集。我想将 y1 相对于 x 绘制为轴上方的条形图,并将 y2 相对于 x 轴下方同一图中的相同 x 绘制,以便两个条形图相互镜像。
下面的图 D 是我正在尝试做的一个示例。
I have a dataset with an x variable and two y1 and y2 variables (3 columns in total). I would like to plot y1 against x as a bar plot above the axis and y2 against the same x in the same plot underneath the x axis so that the two bar plots mirror each other.
Figure D below is an example of what I am trying to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 ggplot,您可以按如下方式进行操作:
设置数据。这里没什么奇怪的,但显然轴下方的值将为负值。
使用 ggplot 和 geom_bar 进行绘图。要防止
geom_bar
汇总数据,请指定stat="identity"
。同样,需要通过指定position="identity"
来禁用堆叠。Using
ggplot
you would go about it as follows:Set up the data. Nothing strange here, but clearly values below the axis will be negative.
Plot using
ggplot
andgeom_bar
. To preventgeom_bar
from summarising the data, specifystat="identity"
. Similarly, stacking needs to be disabled by specifyingposition="identity"
.使用@Andrie的示例数据的基本图形和
lattice
的一些非常小的示例:在基本图形中:
并在
lattice
中:Some very minimal examples for base graphics and
lattice
using @Andrie's example data:In base graphics:
and in
lattice
:这是用 ggplot2 完成的。
首先提供一些数据,然后用melt将两个y放在一起。
然后,制作图表
Perhpas 其他人可以提供带有基和/或格的示例。
华泰
This is done with ggplot2.
First provide some data and put the two y together with melt.
Then, make the graph
Perhpas someone else can provide an example with base and/or lattice.
HTH