简体   繁体   English

如何使用来自 dataframe 的数据在 R 中制作堆叠条 plot?

[英]How to make a stacked bar plot in R with the data from a dataframe?

I have a table that looks as follows:我有一个如下所示的表:

family家庭 max最大限度 mean意思是
OG0000000 OG0000000 1336 1336 348.23423423423424 348.23423423423424
OG0000001 OG0000001 152 152 66.31531531531532 66.31531531531532
OG0000002 OG0000002 104 104 33.85585585585586 33.85585585585586
OG0000003 OG0000003 133 133 32.990990990990994 32.990990990990994
OG0000004 OG0000004 118 118 31.135135135135137 31.135135135135137
OG0000005 OG0000005 79 79 30.83783783783784 30.83783783783784
OG0000006 OG0000006 123 123 30.153153153153152 30.153153153153152
OG0000007 OG0000007 131 131 29.81081081081081 29.81081081081081
OG0000008 OG0000008 129 129 29.684684684684683 29.684684684684683
OG0000009 OG0000009 93 93 29.405405405405407 29.405405405405407
OG0000011 OG0000011 169 169 28.35135135135135 28.35135135135135
OG0000012 OG0000012 73 73 27.56756756756757 27.56756756756757
OG0000013 OG0000013 113 113 27.504504504504503 27.504504504504503
OG0000014 OG0000014 87 87 24.72972972972973 24.72972972972973
OG0000015 OG0000015 106 106 24.675675675675677 24.675675675675677
OG0000017 OG0000017 131 131 23.306306306306308 23.306306306306308
OG0000018 OG0000018 80 80 22.81081081081081 22.81081081081081
OG0000020 OG0000020 234 234 21.324324324324323 21.324324324324323
OG0000024 OG0000024 89 89 20.89189189189189 20.89189189189189

I want to make a bar plot where each bar takes the data in column one('family') as the label and stacks the data in columns two and three for each bar.我想制作一个条形 plot ,其中每个条形将第一列('family')中的数据作为 label 并将每个条形的第二列和第三列中的数据堆叠起来。 Say the datapoints from column two make up the primary data show in the bars and the data from the third column is overlaid over it.假设第二列的数据点构成了条形图中显示的主要数据,第三列的数据覆盖在它上面。

I am trying to do this in ggplot.我正在尝试在 ggplot 中执行此操作。

I have tried the following approach (as taken from this post ):我尝试了以下方法(取自这篇文章):

df_long <- df2 %>% gather(family,counts, 2:3) # here df is the above table as a dataframe
ggplot(df_long, aes(x = family, y = counts, fill = family)) + geom_col(position = position_stack())

This gives an output in the following design, and this is really not what I want.这在以下设计中给出了 output,这真的不是我想要的。

此代码生成的图像

Not sure if the code in the linked post is outdated or if I am doing something wrong.不确定链接帖子中的代码是否已过时或者我做错了什么。

I see some random webpages here (for example) and there showing syntax like this barplot(Values, main = "total revenue", names.arg = months, xlab = "month", ylab = "revenue", col = colors) to make bar plots but here the values object needs to be a vector (which I am not sure I how to pass two sets of data to.)我在这里(例如)看到了一些随机网页,并且那里显示了类似barplot(Values, main = "total revenue", names.arg = months, xlab = "month", ylab = "revenue", col = colors)的语法制作条形图,但这里的values object 需要是一个向量(我不确定如何将两组数据传递给它。)

Searching around on google, it seems stacked bar plots are a bit hard to track down.在谷歌上搜索,似乎堆积的条形图有点难以追踪。 Unfortunately I am not the most familiar with the grammar of graphics , anyone have any pointers?不幸的是,我grammar of graphics不是最熟悉的,有人有什么指点吗?

If you want to stack the max and mean together for each family, then you can do something like this:如果您想将每个家庭的最大值和平均值叠加在一起,那么您可以执行以下操作:

library(tidyverse)

df2 %>%
  pivot_longer(-family) %>%
  ggplot(aes(x = family, y = value, fill = name)) +
  geom_col(position = position_stack()) +
  theme(axis.text.x = element_text(angle = 90))

Output Output

在此处输入图像描述

Another option (rather than mixing stats) would be to use facet_wrap , so that you mean in one graph and max in another:另一种选择(而不是混合统计数据)是使用facet_wrap ,这样你的意思是在一个图中,在另一个图中是 max:

df2 %>%
  pivot_longer(-family) %>%
  ggplot(aes(x = family, y = value)) +
  geom_col(position = position_stack()) +
  scale_y_continuous(breaks = seq(0, 1400, 200),
                     limits = c(0, 1400)) +
  facet_wrap( ~ name, scales = "free_y") +
  theme(axis.text.x = element_text(angle = 90))

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM