按组排序 geom_bar
Order geom_bar by groups
我是 R 的新手。我可以订购图例,但无法按我的意愿订购图形(Produc.、Proc.、Org.、Mark.)。我想像这个图例(图像图表)一样对图表进行排序。我正在使用这段代码。它只能重新排序,但我没有找到好的解决方案。谢谢
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"
))
我是 R 的新手。我可以订购图例,但无法按我的意愿订购图形(Produc.、Proc.、Org.、Mark.)。我想像这个图例(图像图表)一样对图表进行排序。我正在使用这段代码。它只能重新排序,但我没有找到好的解决方案。谢谢
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"
))