如何在R中使用ggplot2创建堆叠条形图并排序堆叠项?

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

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

它产生了这个图像。如何在R中使用ggplot2创建堆叠条形图并排序堆叠项?

我想知道如何按降序对堆进行排序。

我尝试过重新排序的代码,但仍然不起作用。

英文:

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&lt;-data.frame(
Decision =c(&quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;),
variable =c(&quot;Cereals&quot;, &quot;Cereals&quot;, &quot;Forage&quot;, &quot;Forage&quot;, &quot;Fruit&#160;tree&quot;, &quot;Fruit&#160;tree&quot;, &quot;Horticulture&quot;, &quot;Horticulture&quot;, &quot;Industrial&quot;, 
&quot;Industrial&quot;, &quot;Legume&quot;, &quot;Legume&quot;, &quot;Root&#160;crop&quot;, &quot;Root&#160;crop&quot;, &quot;Other&quot;, &quot;Other&quot;, &quot;Total&quot;, &quot;Total&quot;),
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)==&#39;Cereals&#39;] &lt;- &#39;Cereals&#39;
levels(adopt$variable)[levels(adopt$variable)==&#39;Forage&#39;] &lt;- &#39;Forage&#39;
levels(adopt$variable)[levels(adopt$variable)==&#39;Fruit.tree&#39;] &lt;- &#39;Fruit tree&#39;
levels(adopt$variable)[levels(adopt$variable)==&#39;Hoticulture&#39;] &lt;- &#39;Horticulture&#39;
levels(adopt$variable)[levels(adopt$variable)==&#39;Industrial&#39;] &lt;- &#39;Industrial&#39;
levels(adopt$variable)[levels(adopt$variable)==&#39;Legumes&#39;] &lt;- &#39;Legume&#39;
levels(adopt$variable)[levels(adopt$variable)==&#39;Root.crop&#39;] &lt;- &#39;Root crop&#39;
levels(adopt$variable)[levels(adopt$variable)==&#39;Other&#39;] &lt;- &#39;Other&#39;
levels(adopt$variable)[levels(adopt$variable)==&#39;Total&#39;] &lt;- &#39;Total&#39;
library(ggplot2)
##Vertical/Hor adoption graph##
ggplot(adopt, aes(fill = (Decision), y = value, x = variable)) +
geom_bar(stat = &quot;identity&quot;, position = &quot;stack&quot;)+
ylab(&quot;Percentage of Farmers&quot;)+
xlab(&quot;Types of farm&quot;)+
coord_flip()+ #if you toss this line out then you get a vertical stacked bar chart
theme_classic()+
scale_fill_brewer(palette = &quot;Blues&quot;,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=&quot;Times New Roman&quot;))

it produces 如何在R中使用ggplot2创建堆叠条形图并排序堆叠项?

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

如何在R中使用ggplot2创建堆叠条形图并排序堆叠项?

英文:

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 == &quot;Yes&quot;, value, 0), FUN = sum),
x = value
)) +
geom_col() +
ylab(&quot;Percentage of Farmers&quot;) +
xlab(&quot;Types of farm&quot;) +
theme_classic() +
scale_fill_brewer(palette = &quot;Blues&quot;, 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 = &quot;Times New Roman&quot;))

如何在R中使用ggplot2创建堆叠条形图并排序堆叠项?

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

发表评论

匿名网友

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

确定