如何在ggplot的条形图上的单个条形图中按值正确地对分段进行排序(即使从因子更改) [英] How to correctly order segments by value, within an individual bar, on a bar chart in ggplot (even when changing from a factor)

查看:55
本文介绍了如何在ggplot的条形图上的单个条形图中按值正确地对分段进行排序(即使从因子更改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图徒劳地根据值(条形图底部的最大值,最小的条形图)在每个条形图的 内对条形图进行排序在顶部).

我已经对此进行了研究,并认为这应该可行,但是有些不对劲,我找不到问题.我在

似乎条形按字母顺序排列,我知道这是许多类似问题的问题,但是我认为这行代码可以解决该问题: my_repro $ Grp<-reorder(my_repro $ Grp,-my_repro $ Value)

然后我在绘图代码之前添加了这一行,以使Grp不会成为按字母顺序排列的因素: my_repro $ Grp<-as.character(my_repro $ Grp)但我得到相同的情节

任何想法如何解决?

谢谢!

解决方案

尝试使用 forcats 包中的 fct_reorder 函数:

 库(dplyr)图书馆(forcats)my_repro<-my_repro%>%group_by(段)%&%;%mutate(Grp = fct_reorder(Grp,值))# 阴谋ggplot(my_repro,aes(x = Segment,y = Value,fill = Grp))+geom_col() 

I am trying in vain to get the segments within each bar of the bar chart to order based on the value (largest value within a bar on the bottom, smallest on top).

I've researched this and would think this should work, but something is not right and I can't find the issue. I tried this solution here, but no luck.

Here is a reproducible example:

library(dplyr)

my_repro <- structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "2020-04-01", class = "factor"), 
               Grp = c("A", "A", "A", "B", "B", "B", "C", "C", "C", "D", 
                       "D", "D", "E", "E", "E"), Segment = structure(c(1L, 2L, 3L, 
                                                                       1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Seg1", 
                                                                                                                                   "Seg2", "Seg3"), class = "factor", scores = structure(c(Seg1 = NA_real_, 
                                                                                                                                                                                           Seg2 = NA_real_, Seg3 = NA_real_), .Dim = 3L, .Dimnames = list(
                                                                                                                                                                                             c("Seg1", "Seg2", "Seg3")))), Value = c(220, 75, NA, 
                                                                                                                                                                                                                                     NA, 400, NA, 350, NA, NA, 170, NA, NA, 375, 100, 
                                                                                                                                                                                                                                     NA)), row.names = c(NA, -15L), class = c("tbl_df", "tbl", 
                                                                                                                                                                                                                                                                              "data.frame"))





   # Reorder the Group based on the Value 
   my_repro$Grp <- reorder(my_repro$Grp, -my_repro$Value)
   #my_repro$Grp <- as.character(my_repro$Grp)   # I later added this line too, no luck

   # Plot
   ggplot(my_repro, aes(x=Segment, y=Value, fill=Grp)) +
   geom_col()

This gives the following tibble:

# A tibble: 15 x 4
   Date       Grp   Segment Value
   <fct>      <fct> <fct>   <dbl>
 1 2020-04-01 A     Seg1      220
 2 2020-04-01 A     Seg2       75
 3 2020-04-01 A     Seg3       NA
 4 2020-04-01 B     Seg1       NA
 5 2020-04-01 B     Seg2      400
 6 2020-04-01 B     Seg3       NA
 7 2020-04-01 C     Seg1      350
 8 2020-04-01 C     Seg2       NA
 9 2020-04-01 C     Seg3       NA
10 2020-04-01 D     Seg1      170
11 2020-04-01 D     Seg2       NA
12 2020-04-01 D     Seg3       NA
13 2020-04-01 E     Seg1      375
14 2020-04-01 E     Seg2      100
15 2020-04-01 E     Seg3       NA

And the following graph:

It appears that bars are being ordered alphabetically, which I know is an issue on many questions like this, but I thought this line of code would solve it: my_repro$Grp <- reorder(my_repro$Grp, -my_repro$Value)

I then added this line, just before the plot code, so that Grp would not be a factor that was put in alphabetical order: my_repro$Grp <- as.character(my_repro$Grp) but I get the same plot

Any idea how to fix?

Thanks!

解决方案

Try the fct_reorder function from the forcats package:

library(dplyr)
library(forcats)

my_repro <- my_repro %>% 
  group_by(Segment) %>% 
  mutate(Grp = fct_reorder(Grp, Value))

# Plot
ggplot(my_repro, aes(x=Segment, y=Value, fill=Grp)) +
  geom_col()

这篇关于如何在ggplot的条形图上的单个条形图中按值正确地对分段进行排序(即使从因子更改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆