r - ggplot2条形图按一组值排序

标签 r ggplot2 bar-chart

我正在尝试在 ggplot 中订购条形图。 我正在尝试绘制这些并排条形图之一。我有 2 组的数据,我想按一组的值对图进行排序。目前,我的情节是这样的:

Example barplot

我想实现这个:

Expected plot

其中绘图按第 1 组(此处为蓝色)的递减值排序。 我怎么做?我在组内的类别上尝试了 fct_reorder,安排、排序、排序,设法在 ggplot 之外对其进行排序,但结果并非如此……任何帮助将不胜感激!

编辑: 根据建议,我尝试了以下方法:

data %>% 
  ggplot(aes(x = fct_rev(fct_reorder(labels, value)), y = value, fill 
  = variable)) +
   geom_bar(stat="identity", width=.5, position = "dodge")  +
   theme(axis.text.x = element_text(angle = 90, hjust = 1))

我得到了:

Result Plot

这不是我真正想要的。我想按降序对所选(红色条)进行排序。 selected 和 new 显然是一个变量,所以在一列中。如果我必须将它们分开也没关系,我只是不知道该怎么做。

数据:

structure(list(labels = c("3", "16", 
                      "20", "15", "2", 
                      "11", "19", 
                      "8", "1", 
                      "9", "12", "14", 
                      "22", "6", "5", 
                      "18", "17", "23", 
                      "21", "13", "7", 
                      "10", "4", "3", "16", 
                      "20", "15", "2", 
                      "11", "19", 
                      "8", "1", 
                      "9", "12", "14", 
                      "22", "6", "5", 
                      "18", "17", "23", 
                      "21", "13", "7", 
                      "10", "4"), variable = structure(c(1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                          1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                          2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("selected", "new"), class = "factor"), value = c(14.6323031713383, 15.9559892686597, 
                                                                                                                                                                                                  17.8080362595634, 17.9067064016601, 24.0164577112395, 24.8811598509513, 
                                                                                                                                                                                                  26.6357978795688, 26.8956804786175, 29.9814126083425, 32.0991304536091, 
                                                                                                                                                                                                  32.1102536829839, 35.4868462306579, 38.1006034580602, 51.1456293356122, 
                                                                                                                                                                                                  51.3589926258885, 52.1663444535308, 54.5568962268846, 55.046508165816, 
                                                                                                                                                                                                  57.3540750283887, 59.4724704424851, 66.1242468388916, 76.0316429487087, 
                                                                                                                                                                                                  81.2328164785417, 0.852076511173202, 14.5657895883356, 12.879854304722, 
                                                                                                                                                                                                  5.64377267586918, 7.1645313211506, 22.9837800173366, 25.5540168398152, 
                                                                                                                                                                                                  22.2012270424052, 23.9767694081375, 23.8383007367455, 29.8528524241582, 
                                                                                                                                                                                                  35.157323503853, 3.57512478022716, 2.79829414578611, 25.7839377086262, 
                                                                                                                                                                                                  1.61935008218228, 36.4318154009141, 41.9502761903778, 43.6568981148706, 
                                                                                                                                                                                                  50.8270620551394, 25.1245202778254, 17.0173820081843, 32.5450448621647
                                                                                                                                                             )), row.names = c(NA, -46L), class = "data.frame")

最佳答案

在调用 ggplot 之外处理数据以确定您感兴趣的分组变量(标签)的因子顺序。然后将此因子应用于 ggplot 中的标签变量。

library(ggplot2)
library(dplyr)

sel_order <- 
  data %>% 
  filter(variable == "selected") %>% 
  arrange(desc(value)) %>% 
  mutate(labels = factor(labels))


data %>% 
  mutate(labels = factor(labels, levels = sel_order$labels, ordered = TRUE)) %>% 
  ggplot(aes(x = labels, y = value, fill = variable), group = labels) +
  geom_bar(stat="identity", width=.5, position = "dodge")  +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

reprex package 创建于 2020-05-26 (v0.3.0)

关于r - ggplot2条形图按一组值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62011413/

相关文章:

javascript - 在 dc.js 中订购条形图标签(再次)

r - ggplot2:手动添加图例

r - 根据尚不存在的列创建新列

r - R 中的堆积条形图 (ggplot2),其中 y 轴和条形作为计数百分比

python - python绘制两列的总和

javascript - xCharts 按星期几排序

r - 考虑到每个东西运行了多长时间;计算一段时间内运行的事物总数?

r - 绘制所有子组 - 维恩图

R - 聚合县 map 多边形以创建自定义边界

r - 绘图条形图,为着色、不透明度或边框颜色添加第二个因素