按组订购geom_bar

问题描述 投票:0回答:1

我是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"))

enter image description here

r ggplot2 geom-bar
1个回答
0
投票
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"
                    ))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.