简体   繁体   English

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

[英]Sort stacked bar chart by values

This is different to existing questions. 这与现有问题不同。 Other answers pointed to refer to moving entire bars according to a specified order. 其他答案是指按照指定顺序移动整个条形图。 I would like to sort the resulting bars according to one element within the stacked bars. 我想根据堆叠条中的一个元素对结果条进行排序。

I've created a stacked bar plot in R. This is the data set: 我在R中创建了一个堆积的条形图。这是数据集:

dput(Pitch_third)
structure(list(Team = c("Millwall", "Birmingham", "Sheffield United",
                        "Rotherham", "Middlesbrough", "Wigan", "Aston Villa", "Blackburn",
                        "Bolton", "Brentford", "Bristol City", "Leeds", "Preston", "Queens Park Rangers",
                        "Stoke", "Derby", "Ipswich", "Norwich", "West Bromwich Albion",
                        "Nottingham Forest", "Swansea", "Hull", "Reading", "Sheffield Wednesday"), 
               Own_3rd = c(0.25, 0.25, 0.25, 0.29, 0.27, 0.28, 0.28, 0.3, 
                           0.29, 0.28, 0.28, 0.3, 0.28, 0.3, 0.27, 0.28, 0.3, 0.29, 0.29, 
                           0.3, 0.31, 0.3, 0.3, 0.31), 
               Middle_3rd = c(0.41, 0.42, 0.43, 
                              0.4, 0.43, 0.42, 0.44, 0.41, 0.42, 0.42, 0.43, 0.42, 0.42, 0.42, 
                              0.45, 0.45, 0.43, 0.44, 0.44, 0.43, 0.44, 0.45, 0.45, 0.45), 
               Final_3rd = c(0.35, 0.33, 0.32, 0.31, 0.3, 0.3, 0.29, 0.29, 
                             0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.28, 0.27, 0.27, 0.27, 
                             0.27, 0.26, 0.26, 0.25, 0.25, 0.25)), 
          row.names = c(NA, -24L), 
          class = "data.frame")

Then I've created a tibble called Pitch_third from this data. 然后,我根据该数据创建了一个名为Pitch_third Then plotted it with this: 然后用这个绘制它:

Pitch_third %>%
gather(variable, value, Own_3rd:Final_3rd) %>% 
ggplot(aes(x = Team, y = value, fill = variable)) + 
geom_bar(position = "fill", stat = "identity") +
coord_flip()

This is the resulting plot: 这是结果图:

How can I sort the plot so that teams are sorted by the variable Final_3rd rather than alphabetically? 如何对图进行排序,以使团队按变量Final_3rd而不是按字母顺序排序?

I have tried to use arrange() to sort the tibble by Final_3rd but I think gather() might be messing with that after. 我曾尝试使用arrange()排序依据的tibble Final_3rd但我想gather()可能会与后搞乱。

Pitch_third <- arrange(Pitch_third, desc(Final_3rd))

I assume you want this: 我想你想要这个: 这个

Using your data as data 将数据用作data

library(data.table)
library(tidyr)

data2 <- as.data.table(gather(data, variable, value, Own_3rd:Final_3rd))

team_levels <- unique(data2[, Team])[order(data2[variable == "Final_3rd", value])]

data2$Team2 <- factor(data2$Team, levels = team_levels)

ggplot(data = data2,
       aes(x = Team2, y = value, fill = variable)) +
  geom_bar(position = "fill", stat = "identity") +
  coord_flip()

Answer based on: Change the order of a discrete x scale 答案基于: 更改离散x刻度的顺序

I think you did a great job already and you got it right with the arrange , which should be placed after gather . 我认为您已经做的很好, arrange得当,应该放在gather Then a trick could be to get a new factor for Team with the levels sorted according to "Final_3rd" 's values, rather than the default alphabetical order. 然后,一个诀窍可能是为Team获得一个新的因素,使其级别根据"Final_3rd"的值而不是默认的字母顺序进行排序。

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)

Pitch_third %>%
  gather(variable, value, Own_3rd:Final_3rd) %>% 
  arrange(variable, value) %>% 
  # Create a new factor, with its levels sorted according to Final_3rd
  mutate(team_f = factor(Team, levels = .[.$variable == "Final_3rd", "Team"])) %>% 
  # or
  # mutate(team_f = factor(Team, levels = filter(., variable == "Final_3rd") %>% .$Team)) %>%
  ggplot(aes(x = team_f, y = value, fill = variable)) +
  geom_bar(position = "fill", stat = "identity") +
  coord_flip()

Created on 2019-01-17 by the reprex package (v0.2.1) reprex软件包 (v0.2.1)创建于2019-01-17

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

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