如何在R中排列ggplot2堆叠条形图中的堆栈?

iyfjxgzm  于 2023-05-26  发布在  其他
关注(0)|答案(1)|浏览(168)

我想订购这张图。我已经尝试了所有的订购代码,我知道,但它不起作用
这些是我的数据和代码

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

它产生

我想知道如何按降序排列堆栈。
我已经尝试了重新排序代码,但仍然不工作

unguejic

unguejic1#

一种选择是使用reorder,其中我使用一个helper列,我为它设置了例如。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"))

相关问题