我想订购这张图。我已经尝试了所有的订购代码,我知道,但它不起作用
这些是我的数据和代码
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"))
它产生
我想知道如何按降序排列堆栈。
我已经尝试了重新排序代码,但仍然不工作
1条答案
按热度按时间unguejic1#
一种选择是使用
reorder
,其中我使用一个helper列,我为它设置了例如。No
值为零,以便图表按Yes
百分比排序:注意:我使用
FUN=sum
,这在您的情况下并不是真正需要的,但在具有更多类别的更一般的情况下可能有用,....