Search code examples
rlabelpanel

showing labels on ordered grouped panel in ggplot based on a column


I have this data for the graph below. I want to arrange the panel (variable) in orther of another colum (mean) in the data set and show the values of the mean on each panel. How do I go about it?

inten<-data.frame(
  Count=c("1","2","3","4","5","6","1","2","3","4","5","6","1","2","3","4","5","6","1","2","3","4","5","6","1","2","3","4",
          "5","6","1","2","3","4","5","6","1","2","3","4","5","6","1","2","3","4","5","6")
  ,variable =c("Cereals","Cereals", "Cereals", "Cereals", "Cereals", "Cereals", "Forage", "Forage", "Forage", 
             "Forage", "Forage", "Forage", "Fruit Tree", "Fruit Tree", "Fruit Tree", "Fruit Tree", "Fruit Tree", 
             "Fruit Tree", "Horticulture", "Horticulture", "Horticulture", "Horticulture", "Horticulture", "Horticulture", 
             "Industrial", "Industrial", "Industrial", "Industrial", "Industrial", "Industrial", "Legumes", "Legumes", 
             "Legumes", "Legumes", "Legumes", "Legumes", "Root Crop", "Root Crop", "Root Crop", "Root Crop", "Root Crop", 
             "Root Crop", "Other", "Other", "Other", "Other", "Other", "Other")
  ,value =c(30.46,20.67,9.28,3.24,0.74,0.03,28.05,27.64,15.45,4.88,0.81,0.41,30.17,24.75,12.76,4.17,1.82,0.05,28.21,25.56,11.41,4.89,
               2.14,0,34.11,26.75,12.99,4.24,1.3,0.09,28.19,24.47,11.17,0.53,0,0,33.01,20.89,9.71,2.99,0.5,0,30,29.38,18.13,1.88,1.25,0)
  ,mean=c(1.81,1.81,1.81,1.81,1.81,1.81,2.02,2.02,2.02,2.02,2.02,2.02,1.95,1.95,1.95,1.95,1.95,1.95,1.99,1.99,1.99,1.99,1.99,1.99,
           1.89,1.89,1.89,1.89,1.89,1.89,1.75,1.75,1.75,1.75,1.75,1.75,1.76,1.76,1.76,1.76,1.76,1.76,1.94,1.94,1.94,1.94,1.94,1.94)
)



ggplot(inten, aes(fill=Count, y=value, x=Count)) + 
  geom_bar(position="dodge", stat="identity", fill="#EB8B5B") +
  scale_x_discrete(guide = guide_axis(check.overlap = TRUE ))+
  #scale_fill_viridis(discrete = T, option = "E") +
  #ggtitle("Number of PPE used (Intensity) by Respondents") +
  #facet_wrap(variable~., scales = "fixed") + #dividing the graphs into panels
facet_grid(variable~., scales = "fixed", switch = "x")+
  ggplot2::coord_flip()+
  #theme_ipsum() +
  theme_classic()+     #for plain background
  theme(legend.position="right") +
  xlab("Intensity")+
  labs(#title ="My title",
    x = "Intensity (No. of PPE types Used)", 
    y = "Percentage of respondents",
    fill = "Intensity")+
   scale_fill_brewer(palette = "Reds")+
  scale_y_continuous(labels = scales::label_percent(scale = 1, accuracy = 1))+
  theme(strip.background = element_blank(),
    strip.placement='outside')+
  theme(legend.key.size = unit(0.2, 'cm'), #change legend key size
        legend.key.height = unit(0.2, 'cm'), #change legend key height
        legend.key.width = unit(0.4, 'cm'))+ #change legend key width
  theme(strip.text.y.right = element_text(size=11,angle =0))+
  theme(axis.text.x.top = element_text(size=5,  angle = 90))+
  theme(legend.justification='top')+
  theme(legend.text = element_text(size = 10))+
  theme(axis.text.y=element_text(size=9))+
  theme(axis.text.x = element_text(size=11,  angle = 0))+
  theme(axis.title = element_text(size = 15, face = "bold")) +
  theme(legend.title = element_text(size = 15))+
  theme(plot.title = element_text(size=22))+
  theme(text=element_text(size=16,  family="Times New Roman"))

This is the image

enter image description here

How do I arrange the panel order based on this mean.

aggregate(intens$mean, list(intens$variable), FUN=mean)

      Group.1    x
1      Cereals 1.81
2       Forage 2.02
3   Fruit Tree 1.95
4 Horticulture 1.99
5   Industrial 1.89
6      Legumes 1.75
7        Other 1.94
8    Root Crop 1.76

So in the case Forage is the highest with Legumes been the least. How do I show this mean values at where the facets are shown?


Solution

  • ggplot(inten |> 
             transform(variable = forcats::fct_reorder(
               paste(variable, mean, sep = "\n"), -mean)), 
           aes(fill=Count, y=value, x=Count)) + 
      ...
    

    enter image description here