简体   繁体   English

按R中的累积值对堆积的条形图进行排序

[英]Sort stacked bar plot by cumulative value in R

I am pretty new to R and i'm trying to get a stacked bar plot. 我对R很陌生,我正在尝试获得堆积的条形图。 My data looks like this: 我的数据如下所示:

    name    value1  value2
1   A       1118    239
2   B       647     31
3   C       316     1275
4   D       2064    230
5   E       231     85

I need a horizontal bar graph with stacked values, this is as far as i can get with my limited R skills (and most of that is also copy-pasted): 我需要一个带有堆积值的水平条形图,这是我有限的R技能所能达到的(并且大多数也是复制粘贴的):

melted <- melt(data, id.vars=c("name"))

melted$name <- factor(
  melted$name, 
  levels=rev(sort(unique(melted$name))), 
  ordered=TRUE
)

melted2 <- melted[order(melted$value),]

ggplot(melted2, aes(x= name, y = value, fill = variable)) + 
  geom_bar(stat = "identity") +
  coord_flip()

It even took me several hours to get to this point, with witch I am pretty content as far as looks go, this is the produced output 甚至花了我几个小时才到这一点,就女巫而言,我看起来很满意,这是产生的输出

在此处输入图片说明

What I now want to do is to get the bars ordered by summed up value (D is first, followed by C, A, B, E). 我现在想做的是按总和值排序条形图(首先是D,然后是C,A,B,E)。 I googled and tried some reorder and order stuff, but I simply can't get it to behave like I want it to. 我用谷歌搜索并尝试了一些reorderorder东西,但是我根本无法使其表现出我想要的样子。 I'm sure the solution has to be pretty simple, so I hope you guys can help me with this. 我敢肯定解决方案必须非常简单,所以我希望你们可以帮助我。

Thanks in advance! 提前致谢!

Well, I am not down or keeping up with all the latest changes in ggplot, but here is one way you could remedy this 好吧,我并没有拒绝或跟上ggplot的所有最新更改,但这是您可以解决此问题的一种方法

I used your idea to set up the factor levels of name but based on the grouped sums. 我使用您的想法来设置name的因子级别,但基于分组的总和。 You might also find order = variable useful at some point, which will order the bar colors based on the variable, but not needed here 您可能还会发现order = variable在某些时候很有用,它将根据变量对条形颜色进行排序,但此处不需要

data <- read.table(header = TRUE, text = "name    value1  value2
1   A       1118    239
2   B       647     31
3   C       316     1275
4   D       2064    230
5   E       231     85")

library('reshape2')
library('ggplot2')

melted <- melt(data, id.vars=c("name"))

melted <- within(melted, {
  name <- factor(name, levels = names(sort(tapply(value, name, sum))))
})

levels(melted$name)
# [1] "E" "B" "A" "C" "D"

ggplot(melted, aes(x= name, y = value, fill = variable, order = variable)) + 
  geom_bar(stat = "identity") +
  coord_flip()

在此处输入图片说明

Another option would be to use the dplyr package to set up a total column in your data frame and use that to sort. 另一种选择是使用dplyr包在数据框中设置总计列,然后使用该列进行排序。 The approach would look something like this. 该方法看起来像这样。

m <- melted %>% group_by(name) %>% 
     mutate(total = sum(value) ) %>% 
     ungroup() %>%
     arrange(total) %>%
     mutate(name = factor(name, levels = unique(as.character(name))) )

ggplot(m, aes(x = name, y = value, fill = variable)) + geom_bar(stat = 'identity') + coord_flip()

Note that trying below code. 请注意,尝试下面的代码。

  • using tidyr package instead to reshape2 package 使用tidyr包代替reshape2

     library(ggplot2) library(dplyr) library(tidyr) data <- read.table(text = " class value1 value2 A 1118 239 B 647 31 C 316 1275 D 2064 230 E 231 85", header = TRUE) pd <- gather(data, key, value, -class) %>% mutate(class = factor(class, levels = tapply(value, class, sum) %>% sort %>% names)) pd %>% ggplot(aes(x = class, y = value, fill = key, order = class)) + geom_bar(stat = "identity") + coord_flip() 

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

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