gpt4 book ai didi

r - 如何在 facet_nested 中订购 x 轴

转载 作者:行者123 更新时间:2023-12-05 05:08:32 25 4
gpt4 key购买 nike

我创建了一个条形图并根据我的需要排列了 x 轴。但是,一旦我通过 facet_nested 运行它,它就会改变顺序。

这是我的文件的样子:

文件:

drug         P_resistant    G_resistant   
DrugA 18 16
DrugB1 430 425
DrugB2 156 154
DrugB3 0 0
DrugC1 60 56
DrugC2 66 64
DrugC3 113 111
DrugC4 250 248

根据类型、类别和药物顺序排列顺序:

library(ggplot2)
library(tidyr)
library(dplyr)
library(ggrepel)
library(forcats)
library(data.table)
library(ggnomics)
library(scales)
library(survival)
library(tidytext)

file.g <- gather(file, type, value, -drug)

##this is for facet "group"
file.g$group <-c("87.51%","98.85%","98.71%","NA","93.2%","96.9%","98.2%","99%","87.51%","98.85%","98.71%","NA","93.2%","96.9%","98.2%","99%")

##this is for facet "class"
file.g$class <- c(rep("Class A",1),rep("Class B",3),rep("Class C",4),rep("Class A",1),rep("Class B",3),rep("Class C",4))

## the order of drug appearance on x-axis
file.g$drug_order<- c(4,1,2,3,5,6,7,8,4,1,2,3,5,6,7,8)

所以当你查看 file.g 时,它看起来像这样

drug        type           value     group       class      drug_order
DrugA P_resistant 18 87.51% Class A 4
DrugB1 P_resistant 430 98.85% Class B 1
DrugB2 P_resistant 156 98.71% Class B 2
DrugB3 P_resistant 0 NA Class B 3
DrugC1 P_resistant 60 93.2% Class C 5
DrugC2 P_resistant 66 96.9% Class C 6
DrugC3 P_resistant 113 98.2% Class C 7
DrugC4 P_resistant 250 99% Class C 8
DrugA G_resistant 16 87.51% Class A 4
DrugB1 G_resistant 425 98.85% Class B 1
DrugB2 G_resistant 154 98.71% Class B 2
DrugB3 G_resistant 0 NA Class B 3
DrugC1 G_resistant 56 93.2% Class C 5
DrugC2 G_resistant 64 96.9% Class C 6
DrugC3 G_resistant 111 98.2% Class C 7
DrugC4 G_resistant 248 99% Class C 8

下面的代码按照我想要的方式排列 x 轴:

药物 B1、药物 B2、药物 B3、药物 A、药物 C1、药物 C2、药物 C3、药物 C4

file.g$type <- factor(file.g$type, levels=c("P_resistant","G_resistant"))

file.g$class <- factor(file.g$class, levels= c("Class B", "Class A", "Class C"))

##main script
p<-ggplot(file.g, aes(fill=type, x=reorder_within(drug, drug_order, class), y=value)) +
geom_bar(aes(fill = type), stat = "identity", position = "dodge", colour="white") +
geom_text(aes(label=value), position=position_dodge(width=1.2), vjust=-0.5)+
scale_fill_manual(values=c("#af8dc3","#7fbf7b")) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 500)) +
theme(title = element_text(size = 18), legend.text=element_text(size=12), axis.text.x=element_text(size=9), axis.text.y =element_text(size=15)) +
theme(plot.title = element_text(hjust = 0.5))+
scale_x_reordered()

Image 1

但是当我通过嵌套的 facet 运行它时

p+facet_nested(.~class+group, scales= "free_x", space= "free_x")+
theme(strip.text.x = element_text(size = 7.5))

Image2

它将 x 轴顺序更改为

药物 B2、药物 B1、药物 B3、药物 A、药物 C1、药物 C2、药物 C3、药物 C4

我已经坚持这个几个小时了..我感谢任何帮助或想法。

最佳答案

问题不在于 x 轴,x 轴在每个面内只有一个值。更有可能的是,问题出在用于分面的 file.g$classfile.g$group 中的因子级别的排序。这不是 facet_nested 特有的问题,您会遇到与它所基于的 facet_grid 相同的排序问题。

以下是你提到的你想要的顺序:

# Reordering factors
file.g$class <- factor(file.g$class,
levels = c("Class B", "Class A", "Class C"))
file.g$group <- factor(file.g$group,
levels = c("98.85%", "98.71%", "87.51%", "93.2%", "96.9%", "98.2%", "99%"))

# Plotting
ggplot(file.g, aes(fill = type, x = drug, y = value)) +
geom_bar(aes(fill = type), stat = "identity", position = "dodge", colour="white") +
geom_text(aes(label = value), position = position_dodge(width = 1.2), vjust = -0.5)+
scale_fill_manual(values = c("#af8dc3", "#7fbf7b")) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 500)) +
theme(title = element_text(size = 18),
legend.text = element_text(size = 12),
axis.text.x = element_text(size = 9),
axis.text.y =element_text(size = 15),
plot.title = element_text(hjust = 0.5)) +
facet_nested(.~class + group, scales = "free_x", space= "free_x")

enter image description here

关于r - 如何在 facet_nested 中订购 x 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58235789/

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