英文:
How to order stacks in a ggplot2 stacked bar chart in R?
问题
我想要订购这个图表。我已经尝试了我知道的所有排序代码,但它不起作用。
这些是我的数据和代码。
adopt <- data.frame(
Decision = c("No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes"),
variable = c("Cereals", "Cereals", "Forage", "Forage", "Fruit tree", "Fruit tree", "Horticulture", "Horticulture", "Industrial",
"Industrial", "Legume", "Legume", "Root crop", "Root crop", "Other", "Other", "Total", "Total"),
value = c(35.59, 64.41, 22.76, 77.24, 26.28, 73.72, 27.8, 72.2, 20.52, 79.48, 35.64, 64.36, 32.89, 67.11, 19.38, 80.63, 30.52, 69.48)
)
## 图表 1 用于采用
levels(adopt$variable)[levels(adopt$variable) == 'Cereals'] <- 'Cereals'
levels(adopt$variable)[levels(adopt$variable) == 'Forage'] <- 'Forage'
levels(adopt$variable)[levels(adopt$variable) == 'Fruit.tree'] <- 'Fruit tree'
levels(adopt$variable)[levels(adopt$variable) == 'Hoticulture'] <- 'Horticulture'
levels(adopt$variable)[levels(adopt$variable) == 'Industrial'] <- 'Industrial'
levels(adopt$variable)[levels(adopt$variable) == 'Legumes'] <- 'Legume'
levels(adopt$variable)[levels(adopt$variable) == 'Root.crop'] <- 'Root crop'
levels(adopt$variable)[levels(adopt$variable) == 'Other'] <- 'Other'
levels(adopt$variable)[levels(adopt$variable) == 'Total'] <- 'Total'
library(ggplot2)
## 垂直/水平采用图
ggplot(adopt, aes(fill = (Decision), y = value, x = variable)) +
geom_bar(stat = "identity", position = "stack") +
ylab("Percentage of Farmers") +
xlab("Types of farm") +
coord_flip() + # 如果删除这行,则会得到垂直堆叠条形图
theme_classic() +
scale_fill_brewer(palette = "Blues", direction = 1) +
scale_y_continuous(labels = scales::label_percent(scale = 1, accuracy = 1)) +
theme_classic() +
theme(legend.text = element_text(size = 15)) +
theme(legend.title = element_text(size = 17)) +
theme(axis.text.y = element_text(size = 14)) +
theme(axis.text.x = element_text(size = 13, angle = 0)) +
theme(axis.title = element_text(size = 17)) +
theme(plot.title = element_text(size = 22, hjust = 0.5)) +
theme(text = element_text(size = 16, family = "Times New Roman"))
我想知道如何按降序对堆进行排序。
我尝试过重新排序的代码,但仍然不起作用。
英文:
I want to order this graph. I have tried all ordering codes i know but it does not work
these are my data and codes
adopt<-data.frame(
Decision =c("No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes"),
variable =c("Cereals", "Cereals", "Forage", "Forage", "Fruit tree", "Fruit tree", "Horticulture", "Horticulture", "Industrial",
"Industrial", "Legume", "Legume", "Root crop", "Root crop", "Other", "Other", "Total", "Total"),
value=c(35.59,64.41,22.76,77.24,26.28,73.72,27.8,72.2,20.52,79.48,35.64,64.36,32.89,67.11,19.38,80.63,30.52,69.48)
)
##graph 1 for adoption
levels(adopt$variable)[levels(adopt$variable)=='Cereals'] <- 'Cereals'
levels(adopt$variable)[levels(adopt$variable)=='Forage'] <- 'Forage'
levels(adopt$variable)[levels(adopt$variable)=='Fruit.tree'] <- 'Fruit tree'
levels(adopt$variable)[levels(adopt$variable)=='Hoticulture'] <- 'Horticulture'
levels(adopt$variable)[levels(adopt$variable)=='Industrial'] <- 'Industrial'
levels(adopt$variable)[levels(adopt$variable)=='Legumes'] <- 'Legume'
levels(adopt$variable)[levels(adopt$variable)=='Root.crop'] <- 'Root crop'
levels(adopt$variable)[levels(adopt$variable)=='Other'] <- 'Other'
levels(adopt$variable)[levels(adopt$variable)=='Total'] <- 'Total'
library(ggplot2)
##Vertical/Hor adoption graph##
ggplot(adopt, aes(fill = (Decision), y = value, x = variable)) +
geom_bar(stat = "identity", position = "stack")+
ylab("Percentage of Farmers")+
xlab("Types of farm")+
coord_flip()+ #if you toss this line out then you get a vertical stacked bar chart
theme_classic()+
scale_fill_brewer(palette = "Blues",direction = 1)+
scale_y_continuous(labels = scales::label_percent(scale = 1, accuracy = 1))+
theme_classic() +
theme(legend.text = element_text(size = 15))+
theme(legend.title = element_text(size = 17))+
theme(axis.text.y=element_text(size=14))+
theme(axis.text.x = element_text(size=13, angle = 0))+
theme(axis.title = element_text(size = 17))+
theme(plot.title = element_text(size=22, hjust=0.5))+
theme(text=element_text(size=16, family="Times New Roman"))
I want to know how to order the stacks in descending order.
I have tried the reorder code but still not working
答案1
得分: 0
一种选择是在使用 reorder
时,使用一个辅助列,将其中的 No
值设置为零,以便图表按照 Yes
百分比排序:
注意:我使用了 FUN=sum
,在您的情况下并不是真正需要的,但在更一般的情况下,涉及更多类别时可能会有用,... 。
library(ggplot2)
ggplot(adopt, aes(
fill = Decision,
y = reorder(variable, ifelse(Decision == "Yes", value, 0), FUN = sum),
x = value
)) +
geom_col() +
ylab("Percentage of Farmers") +
xlab("Types of farm") +
theme_classic() +
scale_fill_brewer(palette = "Blues", direction = 1) +
scale_x_continuous(labels = scales::label_percent(scale = 1, accuracy = 1)) +
theme_classic() +
theme(legend.text = element_text(size = 15)) +
theme(legend.title = element_text(size = 17)) +
theme(axis.text.y = element_text(size = 14)) +
theme(axis.text.x = element_text(size = 13, angle = 0)) +
theme(axis.title = element_text(size = 17)) +
theme(plot.title = element_text(size = 22, hjust = 0.5)) +
theme(text = element_text(size = 16, family = "Times New Roman"))
英文:
One option would be to use reorder
where I use a helper column for which I set e.g. the No
values to zero so that the chart get ordered by the Yes
percentage:
Note: I use FUN=sum
which is not really needed in your case but might be useful in more general cases with more categories, ... .
library(ggplot2)
ggplot(adopt, aes(
fill = Decision,
y = reorder(variable, ifelse(Decision == "Yes", value, 0), FUN = sum),
x = value
)) +
geom_col() +
ylab("Percentage of Farmers") +
xlab("Types of farm") +
theme_classic() +
scale_fill_brewer(palette = "Blues", direction = 1) +
scale_x_continuous(labels = scales::label_percent(scale = 1, accuracy = 1)) +
theme_classic() +
theme(legend.text = element_text(size = 15)) +
theme(legend.title = element_text(size = 17)) +
theme(axis.text.y = element_text(size = 14)) +
theme(axis.text.x = element_text(size = 13, angle = 0)) +
theme(axis.title = element_text(size = 17)) +
theme(plot.title = element_text(size = 22, hjust = 0.5)) +
theme(text = element_text(size = 16, family = "Times New Roman"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论