首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用forcats包中的fct_relevel()按多个级别对ggplot2中的变量进行重新排序

使用forcats包中的fct_relevel()按多个级别对ggplot2中的变量进行重新排序
EN

Stack Overflow用户
提问于 2021-01-08 09:43:18
回答 1查看 126关注 0票数 1

我需要制作一个图表(使用ggplot2),它按不同类别的值组织条形图。如果你在下面看到,我可以根据我的第一个级别(“非常重要”)进行排序,但我无法获得第二个级别(“重要”)来正确组织-例如:“对农业的热情”应该排在“培养一个健康的工作场所”之上。

代码语言:javascript
复制
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包中的fct_relevel()。但是,即使我将“重要”作为第二个级别添加到排列函数中,它也不被识别。这张图就像我在函数中只包含“非常重要”一样。

代码语言:javascript
复制
alldata1 = alldata %>%
  ungroup() %>%
  arrange(fct_relevel(variable, "Very.important"), value) %>%
  mutate(Category= fct_inorder(Category))

我将我的代码包含在图表中,供您参考。

代码语言:javascript
复制
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

提前谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-08 10:09:18

您可以先在Very.important上对数据执行arrange操作,然后执行Important操作,并指定Category列的因子级别。

代码语言:javascript
复制
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")

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65622334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档