0

I am new in R. I can order the legend but I cannot order the graphic as I want (Produc., Proc., Org., Mark.). I would like to sort the chart like this legend (image chart). I am using this code. It can be only reorder but I don't find a good solution. Thanks

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))

Data example (grado is the name of my dataset)

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

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Rafa Mesa
  • 55
  • 7
  • Could you please include your data in the question to make your question reproducible. Try `dput(grado`. For guidance on how to place reproducible data into a question have a look at https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example? in particular the sections: "Producing a minimal dataset" and "Copy your data". – Peter May 21 '20 at 09:17
  • Please add your dataset as a data frame - not a table - try `dput(head(grado, 20))`. This should make your question reproducible. – Peter May 21 '20 at 09:54
  • I think it's ready. Thanks for your patience in my first post – Rafa Mesa May 21 '20 at 09:54
  • OK well done! the problem now is that you only have data for InnoCat: "Produc." so your graph is not reproducible. You need a sample dataset including data rows from representing each value from InnoCat. How big is your complete dataset? Maybe try filtering your dataset for three values of `Nodo` – Peter May 21 '20 at 10:05

1 Answers1

0

You can convert InnoCat into factor variable with the levels in desired order.

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

You don't have to reverse the legend then.

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

Mohanasundaram
  • 2,889
  • 1
  • 8
  • 18