如何在y轴上创建具有相同数量的多个值的堆叠条形图

时间:2021-10-13 12:48:10

I have tried to create a barplot that I need but I am not able to figure it out. The details are as follows. I have a much bigger data frame but here is a small example of my data frame:

我试图创建一个我需要的条形图,但我无法弄明白。详情如下。我有一个更大的数据框,但这是我的数据框的一个小例子:

SampleNumber = 1:5
Probability_n1 = c(1, 1, 1, 1, 1)
Probability_n2 = c(1.30066666666667e-14, 3.375e-17, 3.698e-16, 2.03733333333333e-17, 2.16866666666667e-19)
Probability_n3 = c(1.243e-28, 5.59956666666667e-35, 4.75333333333333e-34, 1.0569e-34, 1.21333333333333e-37)
Probability_n4 = c(2.266e-43, 4.36333333333333e-56, 4.47933333333333e-53, 1.39033333333333e-53, 2.33566666666667e-56)
Probability_n5 = c(1.7407e-57, 2.59733333333333e-76, 7.461e-73, 2.46833333333333e-72, 2.133e-76)
df = cbind(SampleNumber, Probability_n1, Probability_n2, Probability_n3, Probability_n4, Probability_n5)

df

           SampleNumber   Probability_n1   Probability_n2   Probability_n3   Probability_n4    Probability_n5
[1,]           1                1           1.300667e-14     1.243000e-28      2.266000e-43    1.740700e-57
[2,]           2                1           3.375000e-17     5.599567e-35      4.363333e-56    2.597333e-76
[3,]           3                1           3.698000e-16     4.753333e-34      4.479333e-53    7.461000e-73
[4,]           4                1           2.037333e-17     1.056900e-34      1.390333e-53    2.468333e-72
[5,]           5                1           2.168667e-19     1.213333e-37      2.335667e-56    2.133000e-76

I am trying to create a stacked barplot (one bar for each sample/row in the data frame) where the sample number is on x-axis and the y-axis has the probability such that each of the probabilities (of n=1,2,3,4,5) is filled with a different color in the same bar for each sample.

我正在尝试创建一个堆叠的条形图(数据框中每个样本/行一个条形图),其中样本编号位于x轴上,y轴的概率使得每个概率(n = 1, 2,3,4,5)在每个样品的相同条中填充不同的颜色。

P.S: I understand that in my example, the n=2,3,4,5 values are very small to show on the barplot but my actual data frame has a lot of samples where a different n value has a probability much higher than other n values for that sample.

PS:据我所知,在我的例子中,n = 2,3,4,5值非常小,以显示在条形图上,但我的实际数据框有很多样本,其中不同的n值的概率远高于其他n该样本的值。

1 个解决方案

#1


0  

You can use ggplot for the visualisation:

您可以使用ggplot进行可视化:

library(ggplot2)

First, create a data.frame from your matrix, then reshape it from wide to long using melt() from the data table package which gives you this:

首先,从矩阵创建一个data.frame,然后使用数据表包中的melt()从宽到长重新整形,这样就可以实现:

head(data.table::melt(df, id.vars = "SampleNumber"))
  SampleNumber       variable        value
1            1 Probability_n1 1.000000e+00
2            2 Probability_n1 1.000000e+00
3            3 Probability_n1 1.000000e+00
4            4 Probability_n1 1.000000e+00
5            5 Probability_n1 1.000000e+00
6            1 Probability_n2 1.300667e-14

Then simply create a barplot and use the fill aesthetic to add color to the probabilities.

然后只需创建一个条形图并使用填充美学为概率添加颜色。

df <- data.frame(df)
data.table::melt(df, id.vars = "SampleNumber") %>% 
  ggplot() +
  geom_bar(aes(y = value,
               x = SampleNumber,
               fill = variable),
           stat = 'identity')

The result looks like this: 如何在y轴上创建具有相同数量的多个值的堆叠条形图

结果如下:

#1


0  

You can use ggplot for the visualisation:

您可以使用ggplot进行可视化:

library(ggplot2)

First, create a data.frame from your matrix, then reshape it from wide to long using melt() from the data table package which gives you this:

首先,从矩阵创建一个data.frame,然后使用数据表包中的melt()从宽到长重新整形,这样就可以实现:

head(data.table::melt(df, id.vars = "SampleNumber"))
  SampleNumber       variable        value
1            1 Probability_n1 1.000000e+00
2            2 Probability_n1 1.000000e+00
3            3 Probability_n1 1.000000e+00
4            4 Probability_n1 1.000000e+00
5            5 Probability_n1 1.000000e+00
6            1 Probability_n2 1.300667e-14

Then simply create a barplot and use the fill aesthetic to add color to the probabilities.

然后只需创建一个条形图并使用填充美学为概率添加颜色。

df <- data.frame(df)
data.table::melt(df, id.vars = "SampleNumber") %>% 
  ggplot() +
  geom_bar(aes(y = value,
               x = SampleNumber,
               fill = variable),
           stat = 'identity')

The result looks like this: 如何在y轴上创建具有相同数量的多个值的堆叠条形图

结果如下: