繁体   English   中英

为多个变量制作堆叠条 plot - R 中的 ggplot2

[英]Making a stacked bar plot for multiple variables - ggplot2 in R

我在 ggplot2 中制作堆叠条形图时遇到了一些问题。 我知道如何使用 barplot() 制作一个,但我想使用 ggplot2 因为很容易使条形具有相同的高度(如果我没记错的话,使用 'position = 'fill'')。

我的问题是我有多个变量,我想将 plot 放在一起; 我的数据如下所示:

dfr <- data.frame(
  V1 = c(0.1, 0.2, 0.3),
  V2 = c(0.2, 0.3, 0.2),
  V3 = c(0.3, 0.6, 0.5),
  V4 = c(0.5, 0.1, 0.7),
  row.names = LETTERS[1:3]
)

我想要的是一个 plot,在 X 轴上具有类别 A、B 和 C,并且对于其中的每一个,V1、V2、V3 和 V4 的值在 Y 轴上彼此堆叠。 我见过的大多数图表 plot 在 Y 轴上只有一个变量,但我确信可以以某种方式做到这一点。

我怎么能用 ggplot2 做到这一点? 谢谢!

首先,一些数据操作。 将类别添加为变量并将数据融合为长格式。

dfr$category <- row.names(dfr)
mdfr <- melt(dfr, id.vars = "category")

现在 plot,使用名为variable的变量来确定每个条的填充颜色。

library(scales)
(p <- ggplot(mdfr, aes(category, value, fill = variable)) +
    geom_bar(position = "fill", stat = "identity") +
    scale_y_continuous(labels = percent)
)

(编辑:根据 ggplot2 v0.9 的要求更新代码以使用scales包。)

在此处输入图像描述

请原谅我提出一个新的答案,而我真的只想对@Richie 提供的漂亮解决方案添加评论。 我没有发表评论的最低要求,所以这是我的情况:

... + geom_bar(position="fill")为我的绘图引发了错误,我使用的是 ggplot2 版本 0.9.3.1。 和 reshape2 而不是为熔化而 reshape 。

error_message:
*Mapping a variable to y and also using stat="bin".
  With stat="bin", it will attempt to set the y value to the count of cases in each group.
  This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
  If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
  If you want y to represent values in the data, use stat="identity".
  See ?geom_bar for examples. (Deprecated; last used in version 0.9.2)
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Error in pmin(y, 0) : object 'y' not found*

所以我将其更改为geom_bar(stat='identity')并且它可以工作。

你也可以这样做

library(tidyverse)
dfr %>% rownames_to_column("ID") %>% pivot_longer(!ID) %>%
  ggplot() +
  geom_col(aes(x = ID, y = value, fill = name), position = 'fill')

在此处输入图像描述

暂无
暂无

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

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