facet_wrap中的因子级别顺序 [英] Order of factor levels in facet_wrap

查看:176
本文介绍了facet_wrap中的因子级别顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个facet_wrap,其中facet中的因子顺序是
,基于列因子顺序之一。问题的核心是每个组都具有重复的因子水平,当我绘制时,只有一个因子水平在 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 (否)

#地址否类别
#< fctr> < INT> < CHR>
#1 A 1好
#2 B 1丑陋
#3 C 1丑陋
#4 A 2好
#5 B 2丑陋
# 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(否)%>%
mutate(clas = factor(clas,levels = c (地址,等级=唯一(地址[order(clas)])))%>%
mutate(address = as.character(address))%>%
arrange(否,clas)

地址否clas
#< fctr> < INT> < ORD>
#1 A 1好
#2 B 1丑陋
#3 C 1丑陋
#4 A 2好
#5 C 2坏
# 6 B 2丑陋的
#7 C 3好的
#8 A 3坏的
#9 B 3坏的

正如你所看到的,只有在图中正确排列的 No = 1 组。也许这是因为数据集中只有一个因素级别。

 >等级(df_ex $ address)
[1]ABC

我们如何在每个组中排列因子水平并在facet_wrap中显示它们?根据每个 facet_wrap



中的 clas

ggplot code

$ $ $ $ $ $ $ $ ggplot(df_ex,aes(x =地址,y =,fill = clas))+ #x轴偏置电压依赖性
geom_tile()+
scale_fill_manual(values = c('Good'=green,Bad=蓝色,丑陋=黑色))+
facet_wrap(〜No,ncol = 1,scales =free_x)+
主题(legend.position =top,axis.text .y = element_text(size = 20,angle = 90),axis.text.x = element_text(size = 12,face =bold,color =black),
axis.title.y = element_text (face =bold,size = 20,color =black),
axis.title.x = element_text(face =bold,size = 20,color =black),
strip.text = element_text(size = 26,face =bold),
strip.background = element_rect(fill =#FFFF66,color =black,size = 0.5),
plot.title = element_text(face =bold,color =red,size = 14),
legend.title = element_text(color =black,size = 26,face =b旧的),
legend.text = element_text(color =black,size = 18))+
labs(x =address,y =)

解决方案

此解决方案使每个组都是唯一的,并按照所需顺序排列,然后将名称更改回您的原始名称。

df_ex $ names< -paste(df_ex $ address,df_ex $ clas,df_ex $ no)
df_ex $ names< -factor(df_ex $ names,levels = c(A Good 1, 丑陋的1,丑陋的1,好的2,坏的2,丑陋的2,好的3,坏的3,坏的3))


ggplot(df_ex,aes(x = names,y =,fill = clas))+ #x轴偏置电压依赖性
geom_tile()+
scale_fill_manual(values = c('Good'=green,Bad=Blue,Ugly=black))+
facet_wrap(〜No,ncol = 1,scales =free_x )+
主题(legend.position =top ,axis.text.y = element_text(size = 20,angle = 90),axis.text.x = element_text(size = 12,face =bold,color =black),
axis.title .y = element_text(face =bold,size = 20,color =black),
axis.title.x = element_text(face =bold,size = 20,color =black) ,
strip.text = element_text(size = 26,face =bold),
strip.background = element_rect(fill =#FFFF66,color =black,size = 0.5),
plot.title = element_text(face =bold,color =red,size = 14),
legend.title = element_text(color =black,size = 26,face = ),
legend.text = element_text(color =black,size = 18))+
labs(x =address,y =)+
scale_x_discrete( break = df_ex $ names,labels = df_ex $ address)


I'd like to produce a facet_wrap where the order of factors within facets is based on the one of the column factor order. The heart of the problem is each group has duplicated factor levels and when I do plotting only one factor level is ordered correctly in the facet_wrap. (See the graph below)

I try to order factor levels in each group and each factor level should be ordered correctly inside of each facet.

Here is my attempt

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

Now lets try to sort address levels according to user defined clas column order

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

As you can see only the No=1 group ordered correctly in the plot. Maybe this because only one factor level in the data set.

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

How can we order factor levels in each group and show them in the facet_wrap? according to clas levels in each facet_wrap?

Thanks!

ggplot code

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

解决方案

This solution makes each group unique and arranges in the desired order, then changes the names back to your original names.

df_ex$names<-paste(df_ex$address,df_ex$clas,df_ex$No)
df_ex$names<-factor(df_ex$names,levels=c("A Good 1","B Ugly 1","C Ugly 1", "A Good 2", "C Bad 2", "B Ugly 2", "C Good 3", "A Bad 3", "B Bad 3"))


ggplot(df_ex, aes(x=names,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(breaks=df_ex$names, labels=df_ex$address)

这篇关于facet_wrap中的因子级别顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆