Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 按数字和着色因子排序的堆栈条图_R_Ggplot2_Bar Chart - Fatal编程技术网

R 按数字和着色因子排序的堆栈条图

R 按数字和着色因子排序的堆栈条图,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我有下面的data.frame,名为mydf structure(list(id = structure(c(1L, 1L, 11L, 11L, 11L, 11L, 11L, 11L, 13L, 13L, 15L, 15L, 15L, 15L, 15L, 15L, 19L, 27L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 37L, 38L, 38L, 40L, 42L, 43L, 43L, 46L, 46L, 10L, 10L, 16L,

我有下面的data.frame,名为
mydf

structure(list(id = structure(c(1L, 1L, 11L, 11L, 11L, 11L, 11L, 
11L, 13L, 13L, 15L, 15L, 15L, 15L, 15L, 15L, 19L, 27L, 30L, 30L, 
31L, 31L, 31L, 31L, 31L, 31L, 31L, 37L, 38L, 38L, 40L, 42L, 43L, 
43L, 46L, 46L, 10L, 10L, 16L, 24L, 36L, 5L, 12L, 12L, 12L, 12L, 
16L, 17L, 18L, 18L, 20L, 25L, 25L, 27L, 28L, 28L, 32L, 35L, 41L, 
41L, 41L, 41L, 41L, 41L, 44L, 45L, 45L, 2L, 2L, 3L, 3L, 4L, 4L, 
4L, 4L, 6L, 6L, 7L, 8L, 9L, 10L, 12L, 17L, 17L, 17L, 18L, 18L, 
20L, 22L, 22L, 23L, 23L, 24L, 26L, 26L, 26L, 29L, 29L, 29L, 33L, 
34L, 34L, 35L, 36L, 41L, 42L, 42L, 44L, 44L, 45L, 5L, 7L, 7L, 
9L, 9L, 9L, 9L, 9L, 14L, 14L, 19L, 21L, 21L, 25L, 25L, 25L, 32L, 
32L, 33L, 33L, 33L, 39L, 39L, 40L, 41L, 42L, 42L, 46L, 46L, 47L, 
47L), .Label = c("S003", "S004", "S009", "S016", "S025", "S027", 
"S035", "S036", "S042", "S043", "S045", "S047", "S048", "S049", 
"S050", "S058", "S059", "S061", "S063", "S065", "S067", "S069", 
"S070", "S073", "S076", "S077", "S079", "S083", "S087", "S088", 
"S089", "S093", "S095", "S103", "S109", "S112", "S114", "S119", 
"S121", "S123", "S141", "S142", "S224", "S467", "S486", "S503", 
"S532"), class = "factor"), predclass = c("G1", "G1", "G1", "G1", 
"G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", 
"G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", 
"G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", "G1", "G2", 
"G2", "G2", "G2", "G2", "G3", "G3", "G3", "G3", "G3", "G3", "G3", 
"G3", "G3", "G3", "G3", "G3", "G3", "G3", "G3", "G3", "G3", "G3", 
"G3", "G3", "G3", "G3", "G3", "G3", "G3", "G3", "G4", "G4", "G4", 
"G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", 
"G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", 
"G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", "G4", 
"G4", "G4", "G4", "G4", "G4", "G4", "G4", "G5", "G5", "G5", "G5", 
"G5", "G5", "G5", "G5", "G5", "G5", "G5", "G5", "G5", "G5", "G5", 
"G5", "G5", "G5", "G5", "G5", "G5", "G5", "G5", "G5", "G5", "G5", 
"G5", "G5", "G5", "G5", "G5"), status = c("stable", "stable", 
"stable", "stable", "stable", "stable", "stable", "stable", "stable", 
"stable", "stable", "stable", "stable", "stable", "stable", "stable", 
"unstable", "unstable", "stable", "stable", "stable", "stable", 
"stable", "stable", "stable", "stable", "stable", "stable", "stable", 
"stable", "unstable", "unstable", "stable", "stable", "unstable", 
"unstable", "unstable", "unstable", "unstable", "unstable", "unstable", 
"unstable", "unstable", "unstable", "unstable", "unstable", "unstable", 
"unstable", "unstable", "unstable", "unstable", "unstable", "unstable", 
"unstable", "stable", "stable", "unstable", "unstable", "unstable", 
"unstable", "unstable", "unstable", "unstable", "unstable", "unstable", 
"unstable", "unstable", "stable", "stable", "stable", "stable", 
"stable", "stable", "stable", "stable", "stable", "stable", "unstable", 
"stable", "unstable", "unstable", "unstable", "unstable", "unstable", 
"unstable", "unstable", "unstable", "unstable", "stable", "stable", 
"stable", "stable", "unstable", "stable", "stable", "stable", 
"stable", "stable", "stable", "unstable", "stable", "stable", 
"unstable", "unstable", "unstable", "unstable", "unstable", "unstable", 
"unstable", "unstable", "unstable", "unstable", "unstable", "unstable", 
"unstable", "unstable", "unstable", "unstable", "stable", "stable", 
"unstable", "stable", "stable", "unstable", "unstable", "unstable", 
"unstable", "unstable", "unstable", "unstable", "unstable", "stable", 
"stable", "unstable", "unstable", "unstable", "unstable", "unstable", 
"unstable", "stable", "stable")), row.names = c(1L, 2L, 28L, 
29L, 30L, 31L, 32L, 33L, 39L, 40L, 43L, 44L, 45L, 46L, 47L, 48L, 
59L, 80L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 108L, 
109L, 110L, 113L, 124L, 128L, 129L, 136L, 139L, 25L, 27L, 50L, 
69L, 106L, 12L, 34L, 36L, 37L, 38L, 49L, 54L, 56L, 58L, 62L, 
72L, 73L, 79L, 81L, 82L, 97L, 104L, 115L, 116L, 117L, 120L, 121L, 
122L, 131L, 133L, 135L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 13L, 
14L, 16L, 18L, 19L, 26L, 35L, 51L, 52L, 53L, 55L, 57L, 61L, 65L, 
66L, 67L, 68L, 70L, 76L, 77L, 78L, 83L, 84L, 85L, 99L, 102L, 
103L, 105L, 107L, 119L, 123L, 125L, 130L, 132L, 134L, 11L, 15L, 
17L, 20L, 21L, 22L, 23L, 24L, 41L, 42L, 60L, 63L, 64L, 71L, 74L, 
75L, 95L, 96L, 98L, 100L, 101L, 111L, 112L, 114L, 118L, 126L, 
127L, 137L, 138L, 140L, 141L), class = "data.frame")
我正试图将其绘制为一个堆栈条形图,以
status
为面。我想按
predclass
顺序排列条形图,因此G1将位于顶部,然后是G2和…,在每个组中,条形图也应按其计数排序

我已经找了很多,但找不到好的解决方案。有人有主意吗? 这是我的代码:

ggplot(mydf, aes(x = forcats::fct_infreq(id),fill = predclass)) + 
  geom_bar(stat = "count", width = 0.85) + 
  facet_wrap(.~ status, ncol = 2, strip.position="left",scales = "free") + 
  coord_flip() +
  theme_classic()

我不完全确定订单应该如何安排在右侧,但我认为下面的代码给出了您想要的。诀窍是首先以正确的顺序获得
id
因子

库(tidyverse)
列夫%
as_tible()%>%
变异(predclass=as.ordered(predclass))%>%
分组依据(id)%>%
突变(顺序id=min(predclass))%>%
分组依据(订单id,id)%>%
汇总(n=n())%>%
排列(描述(订单id),n)%>%
拉力(id)%>%
as.character()
mydf%>%
变异(id=fct_relevel(id,lev),
predclass=已订购(predclass)%%>%fct\U版本)%%>%
ggplot(aes(x=id,fill=predclass))+
几何图形条(stat=“count”,宽度=0.85)+
面_包裹(~status,scales=“free”)+
coord_flip()+
主题(经典)

太棒了!谢谢。我是否可以颠倒顺序,使小条首先出现在每组的顶部?