英文:
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"))
这是图像:
我该如何根据这个均值排列面板的顺序?
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<-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
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 |>
transform(variable = forcats::fct_reorder(
paste(variable, mean, sep = "\n"), -mean)),
aes(fill=Count, y=value, x=Count)) +
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论