繁体   English   中英

按组排序 geom_bar

[英]Order geom_bar by groups

我是 R 的新手。 我可以订购图例,但无法按我的意愿订购图形(生产、加工、组织、标记)。 我想像这个图例(图像图表)一样对图表进行排序。 我正在使用此代码。 它只能重新排序,但我没有找到一个好的解决方案。 谢谢

ggplot(grado,aes(Label,Grado,fill=as.factor(InnoCat)))+
  geom_bar(position = "dodge",stat="identity")+
  facet_wrap(~InnoCat,nrow=4, scales = "free_y")+
  coord_trans() +
  scale_fill_manual("Leyenda",
                    values = c(
                      "Mark." = "#d982d4",
                      "Org." = "#667ee8",
                      "Proc." = "#53c24f",
                      "Produc." = "#e6914c"
                    )) +
  guides(fill = guide_legend(reverse = TRUE))

数据示例(grado 是我的数据集的名称)

structure(list(Nodo = c("P1", "P2", "P3", "P4", "P5", "P6", "P7", 
"P8", "P9", "P10", "P11", "Cooperativas", "Cursos o ferias", 
"Empresas", "Familia y Amigos", "Grupos de Consumo", "Instituciones", 
"Investigación", "Organizaciones", "Técnicos"), Grado = c(0.2, 
0.2, 0.4, 0.4, 0.2, 0.2, 0.4, 0.2, 0.2, 0.2, 0.2, 0.45, NA, 0.45, 
NA, NA, NA, NA, 0.09, 0.18), InnoCat = c("Produc.", "Produc.", 
"Produc.", "Produc.", "Produc.", "Produc.", "Produc.", "Produc.", 
"Produc.", "Produc.", "Produc.", "Produc.", "Produc.", "Produc.", 
"Produc.", "Produc.", "Produc.", "Produc.", "Produc.", "Produc."
), Label = c("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", 
"P9", "P10", "P11", "COOP", "EVEBT", "EMPR", "FA", "GC", "ADMON", 
"INV", "ORG", "TEC")), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

在此处输入图像描述

您可以将 InnoCat 转换为具有所需顺序的级别的因子变量。

grado$InnoCat <- factor(grado$InnoCat, levels = c("Produc.", "Proc.", "Org.", "Mark."))

那时您不必反转图例。

ggplot(grado,aes(Label,Grado,fill=InnoCat))+
  geom_bar(position = "dodge",stat="identity")+
  facet_wrap(~InnoCat,nrow=4, scales = "free_y")+
  coord_trans() +
  scale_fill_manual("Leyenda",
                    values = c(
                      "Mark." = "#d982d4",
                      "Org." = "#667ee8",
                      "Proc." = "#53c24f",
                      "Produc." = "#e6914c"
                    ))

在此处输入图像描述

暂无
暂无

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

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