如何为 R 中 x 轴镜像的两个变量创建条形图?

发布于 2024-11-29 05:15:53 字数 205 浏览 1 评论 0原文

我有一个包含 x 变量和两个 y1 和 y2 变量(总共 3 列)的数据集。我想将 y1 相对于 x 绘制为轴上方的条形图,并将 y2 相对于 x 轴下方同一图中的相同 x 绘制,以便两个条形图相互镜像。

下面的图 D 是我正在尝试做的一个示例。

图 **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.

Figure **D**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倦话 2024-12-06 05:15:53

使用 ggplot,您可以按如下方式进行操作:

设置数据。这里没什么奇怪的,但显然轴下方的值将为负值。

dat <- data.frame(
    group = rep(c("Above", "Below"), each=10),
    x = rep(1:10, 2),
    y = c(runif(10, 0, 1), runif(10, -1, 0))
)

使用 ggplot 和 geom_bar 进行绘图。要防止 geom_bar 汇总数据,请指定 stat="identity"。同样,需要通过指定 position="identity" 来禁用堆叠。

library(ggplot2)
ggplot(dat, aes(x=x, y=y, fill=group)) + 
  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.

dat <- data.frame(
    group = rep(c("Above", "Below"), each=10),
    x = rep(1:10, 2),
    y = c(runif(10, 0, 1), runif(10, -1, 0))
)

Plot using ggplot and geom_bar. To prevent geom_bar from summarising the data, specify stat="identity". Similarly, stacking needs to be disabled by specifying position="identity".

library(ggplot2)
ggplot(dat, aes(x=x, y=y, fill=group)) + 
  geom_bar(stat="identity", position="identity")

enter image description here

洛阳烟雨空心柳 2024-12-06 05:15:53

使用@Andrie的示例数据的基本图形和lattice的一些非常小的示例:

dat <- data.frame(
    group = rep(c("Above", "Below"), each=10),
    x = rep(1:10, 2),
    y = c(runif(10, 0, 1), runif(10, -1, 0))
)

在基本图形中:

plot(c(0,12),range(dat$y),type = "n")
barplot(height = dat$y[dat$group == 'Above'],add = TRUE,axes = FALSE)
barplot(height = dat$y[dat$group == 'Below'],add = TRUE,axes = FALSE)

bar_base

并在lattice中:

barchart(y~x,data = dat, origin = 0, horizontal = FALSE)

在此处输入图像描述

Some very minimal examples for base graphics and lattice using @Andrie's example data:

dat <- data.frame(
    group = rep(c("Above", "Below"), each=10),
    x = rep(1:10, 2),
    y = c(runif(10, 0, 1), runif(10, -1, 0))
)

In base graphics:

plot(c(0,12),range(dat$y),type = "n")
barplot(height = dat$y[dat$group == 'Above'],add = TRUE,axes = FALSE)
barplot(height = dat$y[dat$group == 'Below'],add = TRUE,axes = FALSE)

bar_base

and in lattice:

barchart(y~x,data = dat, origin = 0, horizontal = FALSE)

enter image description here

我ぃ本無心為│何有愛 2024-12-06 05:15:53

这是用 ggplot2 完成的。
首先提供一些数据,然后用melt将两个y放在一起。

library(ggplot2)

dtfrm <- data.frame(x = 1:10, y1 = rnorm(10, 50, 10), y2 = -rnorm(10, 50, 10))
dtfrm.molten <- melt(dtfrm, id = "x")

然后,制作图表

ggplot(dtfrm.molten, aes(x , value, fill = variable)) + 
  geom_bar(stat = "identity", position = "identity")

Perhpas 其他人可以提供带有基和/或格的示例。

华泰

This is done with ggplot2.
First provide some data and put the two y together with melt.

library(ggplot2)

dtfrm <- data.frame(x = 1:10, y1 = rnorm(10, 50, 10), y2 = -rnorm(10, 50, 10))
dtfrm.molten <- melt(dtfrm, id = "x")

Then, make the graph

ggplot(dtfrm.molten, aes(x , value, fill = variable)) + 
  geom_bar(stat = "identity", position = "identity")

Perhpas someone else can provide an example with base and/or lattice.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文