[英]Reorder variables in ggplot2 by more than one level using fct_relevel() from forcats package
我需要制作一個圖表(使用 ggplot2),按不同類別的值組織條形圖。 如果您在下面看到,我能夠按我的第一級(“非常重要”)進行排序,但我無法獲得第二級(“重要”)以正確組織 - 例如:“對農業的熱情”應該結束高於“培養健康的工作場所”。
library(ggplot2)
library(dplyr)
library(forcats)
library(reshape)
data<- data.frame(Category=c("Open attitude","Flexible","Interest in learning","Passion for farming",
"Dependable business networks","Community ties", "Reliable crew",
"Family support", "Responsive government", "Protect natural resources and biodiversity",
"Build healthy soil", "Diversify farm products", "Minimize external inputs",
"Water-use efficiency", "Effective planning and monitoring", "Cultivating a healthy workplace",
"Diversifying markets and venues", "Focusing on recurrent customers",
"Appropriate equipment and infrastructure", "Financial leeway and capacity"),
Very.important=c(78.57,85.71,85.71,92.86,100.00,85.71,78.57,64.29,50.00,57.14,
100.00,64.29,57.14,57.14,78.57,92.86,71.43,71.43,64.29,71.43),
Important=c(21.43,14.29,14.29,7.14,0.00,14.29,21.43,35.71,21.43,35.71,
0.00,35.71,28.57,35.71,21.43,0.00,14.29,7.14,28.57,21.43),
Slightly.important=c(0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,28.57,7.14,
0.00,0.00,14.29,7.14,0.00,7.14,14.29,21.43,7.14,7.14),
Not.important=c(0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,
0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00))
data
alldata <- melt(data)
首先,為了在新的 data.frame 中組織變量,我使用了來自 forcats package 的 fct_relevel()。 但是,即使我在排列 function 中添加“重要”作為第二級,它也沒有被識別。 該圖與我在 function 中僅包含“非常重要”的結果相同。
alldata1 = alldata %>%
ungroup() %>%
arrange(fct_relevel(variable, "Very.important"), value) %>%
mutate(Category= fct_inorder(Category))
我包括我的圖表代碼供您參考。
mycolors <- c('#0570b0','#74a9cf','#bdc9e1','#f1eef6')
ALLres <- ggplot(data = alldata1, aes(x =Category, y = value, fill = variable)) +
labs(y="Percentage", x = "") +
geom_col(width = 0.7, position = position_stack(reverse = T)) +
coord_flip() +
theme_bw() +
theme(text = element_text(size = rel(3), colour = "black"), # x-label
axis.text.y = element_text(size = rel(3.5), colour = "black"),
axis.text.x = element_text(size = rel(3), colour = "black")) +
theme(legend.text = element_text(size = rel(3))) + #legend size
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.position="bottom") +
scale_fill_manual (values=mycolors,
name = "Response",
labels = c("Very Important", "Important",
"Slightly Important", "Not Important"))
ALLres
先感謝您!
您可以先在Very.important
上arrange
數據,然后在Important
上排列數據,並分配Category
列的因子水平。
library(tidyverse)
mycolors <- rev(c('#0570b0','#74a9cf','#bdc9e1','#f1eef6'))
data %>%
arrange(Very.important, Important) %>%
mutate(Category = factor(Category, Category)) %>%
pivot_longer(cols = -Category) %>%
ggplot(aes(x=Category, y = value, fill = name)) +
labs(y="Percentage", x = "") +
geom_col(width = 0.7) +
coord_flip() +
theme_bw() +
theme(text = element_text(size = rel(3), colour = "black"), # x-label
axis.text.y = element_text(size = rel(3.5), colour = "black"),
axis.text.x = element_text(size = rel(3), colour = "black")) +
theme(legend.text = element_text(size = rel(3))) + #legend size
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.position="bottom") +
scale_fill_manual (values=mycolors,
name = "Response")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.