gpt4 book ai didi

r - facet_wrap 中因子级别的顺序

转载 作者:行者123 更新时间:2023-12-01 19:07:15 24 4
gpt4 key购买 nike

我想生成一个facet_wrap,其中构面内因子的顺序是基于列因子顺序之一。问题的核心是每个组都有重复的因子水平,当我绘制时,facet_wrap 中仅正确排序了一个因子水平。 (见下图)

我尝试对每个组中的因子级别进行排序,并且每个因子级别应在每个方面内正确排序。

这是我的尝试

df_pattern<- data.frame(address = rep(rep(LETTERS[1:3]),3)) 

df_TP <- data.frame(No=rep(seq(1:3)),
clas=c("Good","Bad","Ugly"),stringsAsFactors = F)

set.seed(12)
df_ex <- df_pattern%>%
mutate(No=rep(seq(1:3),each=3))%>%
left_join(df_TP)%>%
mutate(clas=sample(clas))%>%
group_by(No)

# address No clas
# <fctr> <int> <chr>
# 1 A 1 Good
# 2 B 1 Ugly
# 3 C 1 Ugly
# 4 A 2 Good
# 5 B 2 Ugly
# 6 C 2 Bad
# 7 A 3 Bad
# 8 B 3 Bad
# 9 C 3 Good

现在让我们尝试根据用户定义的 clas 列顺序对地址级别进行排序

set.seed(12)
df_ex <- df_pattern%>%
mutate(No=rep(seq(1:3),each=3))%>%
left_join(df_TP)%>%
mutate(clas=sample(clas))%>%
group_by(No)%>%
mutate(clas=factor(clas,levels=c("Good","Bad","Ugly")))%>%
mutate(address=factor(address,levels=unique(address[order(clas)])))%>%
mutate(address=as.character(address))%>%
arrange(No,clas)

address No clas
# <fctr> <int> <ord>
# 1 A 1 Good
# 2 B 1 Ugly
# 3 C 1 Ugly
# 4 A 2 Good
# 5 C 2 Bad
# 6 B 2 Ugly
# 7 C 3 Good
# 8 A 3 Bad
# 9 B 3 Bad

正如您所看到的,图中只有 No=1 组排序正确。也许这是因为数据集中只有一个因素水平。

> levels(df_ex$address)
[1] "A" "B" "C"

我们如何对每组中的因子水平进行排序并在facet_wrap中显示它们?根据每个facet_wrap中的class级别?

谢谢!

ggplot代码

ggplot(df_ex, aes(x=address,y="",fill=clas)) + #x axis bias voltage dependence
geom_tile() +
scale_fill_manual(values=c('Good'="green","Bad"="Blue","Ugly"="black"))+
facet_wrap(~No,ncol=1,scales = "free_x")+
theme(legend.position = "top",axis.text.y = element_text(size = 20,angle = 90),axis.text.x = element_text(size=12,face="bold",colour = "black"),
axis.title.y = element_text(face="bold",size = 20, colour = "black"),
axis.title.x = element_text(face="bold",size = 20 , colour = "black"),
strip.text = element_text(size=26, face="bold"),
strip.background = element_rect(fill="#FFFF66", colour="black", size=0.5),
plot.title=element_text(face="bold",color="red",size=14),
legend.title = element_text(colour="black", size=26,face="bold"),
legend.text = element_text(colour="black", size=18))+
labs(x = "address",y = "")

enter image description here

最佳答案

这个老问题已经有 accepted answer 。但由于它被用作欺骗目标,我觉得有必要提出一个稍微改进且更简洁的变体。

它基于 ggplot2 包的最新增强功能,即 scale_x_discrete()labels 参数,以及 Hadley 的 forcats 软件包于 2016 年 8 月发布到 CRAN。所提出的解决方案增强了 accepted answer使用 this answer 中的 Material .

准备数据

OP 提供的

df_ex 需要修改为包含一个变量,该变量保证所有方面的整体排序顺序:

library(dplyr)   # version 0.5.0 used 
df_ex <- df_ex %>% mutate(ordered = paste0(No, address) %>%
forcats::fct_inorder())

df_ex 的附加列现在如下所示:

  address    No   clas ordered
<chr> <int> <fctr> <fctr>
1 A 1 Good 1A
2 B 1 Ugly 1B
3 C 1 Ugly 1C
4 A 2 Good 2A
5 C 2 Bad 2C
6 B 2 Ugly 2B
7 C 3 Good 3C
8 A 3 Bad 3A
9 B 3 Bad 3B

由于 df_ex 已使用 arrange() 按所需顺序排序,fct_inorder() 返回新列 ordered 关卡的顺序与首次出现的顺序相同。

绘图

x 轴上绘制的是 ordered,而不是 addressfacet_wrap() 的参数 scales = "free_x" 确保未使用的级别将从构面中删除。但是,需要通过向 scale_x_discrete()labels 参数提供命名向量来替换 x 轴上的标签。

library(ggplot2)   # version 2.2.1 used
ggplot(df_ex, aes(x=ordered,y="",fill=clas)) + #x axis bias voltage dependence
geom_tile() +
scale_fill_manual(values=c('Good'="green","Bad"="Blue","Ugly"="black"))+
facet_wrap(~No,ncol=1,scales = "free_x")+
theme(legend.position = "top",axis.text.y = element_text(size = 20,angle = 90),axis.text.x = element_text(size=12,face="bold",colour = "black"),
axis.title.y = element_text(face="bold",size = 20, colour = "black"),
axis.title.x = element_text(face="bold",size = 20 , colour = "black"),
strip.text = element_text(size=26, face="bold"),
strip.background = element_rect(fill="#FFFF66", colour="black", size=0.5),
plot.title=element_text(face="bold",color="red",size=14),
legend.title = element_text(colour="black", size=26,face="bold"),
legend.text = element_text(colour="black", size=18))+
labs(x = "address",y = "") +
scale_x_discrete(labels = setNames(df_ex$address, df_ex$ord)) +

enter image description here

关于r - facet_wrap 中因子级别的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41199496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com