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

时间:2021-09-29 14:57:15

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.

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

Figure D below is an example of what I am trying to do.

下面的图D是我想要做的一个例子。

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

3 个解决方案

#1


15  

Using ggplot you would go about it as follows:

使用ggplot你可以按如下方式进行:

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".

使用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")

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

#2


9  

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

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

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)

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

and in lattice:

并在格子中:

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

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

#3


0  

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

这是通过ggplot2完成的。首先提供一些数据并将两者放在一起融化。

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.

Perhpas别人可以提供基础和/或格子的例子。

HTH

HTH

#1


15  

Using ggplot you would go about it as follows:

使用ggplot你可以按如下方式进行:

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".

使用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")

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

#2


9  

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

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

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)

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

and in lattice:

并在格子中:

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

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

#3


0  

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

这是通过ggplot2完成的。首先提供一些数据并将两者放在一起融化。

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.

Perhpas别人可以提供基础和/或格子的例子。

HTH

HTH