R语言 基于列在ggplot中显示有序分组面板上的标签

tkclm6bt  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(147)

我有这个数据下图。我想在数据集中将面板(变量)安排在另一列(平均值)中,并在每个面板上显示平均值。我该怎么做?

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

这就是图像

如何根据此平均值安排面板顺序。

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

因此,在这种情况下,牧草是最高的豆类是最少的。如何在显示面的位置显示此平均值?

yb3bgrhw

yb3bgrhw1#

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

相关问题