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

huangapple go评论57阅读模式
英文:

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

问题

我有这个图表下面的数据。我想按数据集中另一列(均值)的顺序排列面板(变量),并在每个面板上显示均值的值。我该如何操作?

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)) +
  facet_grid(variable ~ ., scales = "fixed", switch = "x") +
  ggplot2::coord_flip() +
  theme_classic() +
  theme(legend.position = "right") +
  xlab("Intensity") +
  labs(
    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'), legend.key.height = unit(0.2, 'cm'), legend.key.width = unit(0.4, 'cm')) +
  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"))

这是图像:

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

我该如何根据这个均值排列面板的顺序?

aggregate(inten$mean, list(inten$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

所以在这种情况下,Forage的均值最高,Legumes的均值最低。我如何在显示分面的位置显示这些均值?

英文:

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&lt;-data.frame(
Count=c(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,
&quot;5&quot;,&quot;6&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;)
,variable =c(&quot;Cereals&quot;,&quot;Cereals&quot;, &quot;Cereals&quot;, &quot;Cereals&quot;, &quot;Cereals&quot;, &quot;Cereals&quot;, &quot;Forage&quot;, &quot;Forage&quot;, &quot;Forage&quot;, 
&quot;Forage&quot;, &quot;Forage&quot;, &quot;Forage&quot;, &quot;Fruit Tree&quot;, &quot;Fruit Tree&quot;, &quot;Fruit Tree&quot;, &quot;Fruit Tree&quot;, &quot;Fruit Tree&quot;, 
&quot;Fruit Tree&quot;, &quot;Horticulture&quot;, &quot;Horticulture&quot;, &quot;Horticulture&quot;, &quot;Horticulture&quot;, &quot;Horticulture&quot;, &quot;Horticulture&quot;, 
&quot;Industrial&quot;, &quot;Industrial&quot;, &quot;Industrial&quot;, &quot;Industrial&quot;, &quot;Industrial&quot;, &quot;Industrial&quot;, &quot;Legumes&quot;, &quot;Legumes&quot;, 
&quot;Legumes&quot;, &quot;Legumes&quot;, &quot;Legumes&quot;, &quot;Legumes&quot;, &quot;Root Crop&quot;, &quot;Root Crop&quot;, &quot;Root Crop&quot;, &quot;Root Crop&quot;, &quot;Root Crop&quot;, 
&quot;Root Crop&quot;, &quot;Other&quot;, &quot;Other&quot;, &quot;Other&quot;, &quot;Other&quot;, &quot;Other&quot;, &quot;Other&quot;)
,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=&quot;dodge&quot;, stat=&quot;identity&quot;, fill=&quot;#EB8B5B&quot;) +
scale_x_discrete(guide = guide_axis(check.overlap = TRUE ))+
#scale_fill_viridis(discrete = T, option = &quot;E&quot;) +
#ggtitle(&quot;Number of PPE used (Intensity) by Respondents&quot;) +
#facet_wrap(variable~., scales = &quot;fixed&quot;) + #dividing the graphs into panels
facet_grid(variable~., scales = &quot;fixed&quot;, switch = &quot;x&quot;)+
ggplot2::coord_flip()+
#theme_ipsum() +
theme_classic()+     #for plain background
theme(legend.position=&quot;right&quot;) +
xlab(&quot;Intensity&quot;)+
labs(#title =&quot;My title&quot;,
x = &quot;Intensity (No. of PPE types Used)&quot;, 
y = &quot;Percentage of respondents&quot;,
fill = &quot;Intensity&quot;)+
scale_fill_brewer(palette = &quot;Reds&quot;)+
scale_y_continuous(labels = scales::label_percent(scale = 1, accuracy = 1))+
theme(strip.background = element_blank(),
strip.placement=&#39;outside&#39;)+
theme(legend.key.size = unit(0.2, &#39;cm&#39;), #change legend key size
legend.key.height = unit(0.2, &#39;cm&#39;), #change legend key height
legend.key.width = unit(0.4, &#39;cm&#39;))+ #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=&#39;top&#39;)+
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 = &quot;bold&quot;)) +
theme(legend.title = element_text(size = 15))+
theme(plot.title = element_text(size=22))+
theme(text=element_text(size=16,  family=&quot;Times New Roman&quot;))

This is the image

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

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?

答案1

得分: 2

ggplot(inten |&gt; 
         transform(variable = forcats::fct_reorder(
           paste(variable, mean, sep = &quot;\n&quot;), -mean)), 
       aes(fill=Count, y=value, x=Count)) + 
  ...
英文:
ggplot(inten |&gt; 
transform(variable = forcats::fct_reorder(
paste(variable, mean, sep = &quot;\n&quot;), -mean)), 
aes(fill=Count, y=value, x=Count)) + 
...

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

huangapple
  • 本文由 发表于 2023年5月30日 04:12:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定