使用forcats包中的fct_relevel()将ggplot2中的变量重新排序一个以上级别

使用forcats包中的fct_relevel()将ggplot2中的变量重新排序一个以上级别,r,ggplot2,plot,dplyr,forcats,R,Ggplot2,Plot,Dplyr,Forcats,我需要制作一个图表(使用ggplot2),根据不同类别的值来组织条形图。如果你在下面看到,我能够按照我的第一个级别(“非常重要”)进行排序,但我没有能够按照第二个级别(“重要”)进行正确组织——例如:“对农业的热情”应该高于“培养健康的工作场所” library(ggplot2) library(dplyr) library(forcats) library(reshape) data<- data.frame(Category=c("Open attitude",&

我需要制作一个图表(使用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)
我包括我的图形代码供您参考

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

mycolors您可以
首先在
Very.important
上排列
数据,然后在
important
上分配
类别
列的因子级别

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")
库(tidyverse)
霉色素%
安排(非常重要,重要)%>%
突变(类别=因子(类别,类别))%>%
枢轴长度(cols=-类别)%>%
ggplot(aes(x=类别,y=值,填充=名称))+
实验室(y=”百分比“,x=”)+
几何坐标(宽度=0.7)+
coord_flip()+
主题_bw()+
主题(文本=元素)文本(大小=相对(3),颜色=“黑色”),#x标签
axis.text.y=元素\文本(大小=相对(3.5),颜色=“黑色”),
axis.text.x=元素\文本(大小=相对(3),颜色=“黑色”))+
主题(legend.text=element_text(size=rel(3))+#图例大小
主题(panel.grid.major=element\u blank(),panel.grid.minor=element\u blank())+
主题(legend.position=“bottom”)+
刻度填充手册(数值=颜色,
name=“响应”)

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