使用另一个分类变量订购X轴分类变量

发布于 2025-02-03 17:36:19 字数 1433 浏览 4 评论 0原文

我有一个数据框,如下所示:

Class       ID   Stage   Abundance          Substrat
OTUA      rep1  A-X1    123                  G
OTUA      rep2  A-X1    234                  PC
OTUA      rep3  A-X1    [numerical values]   [only two categorical values]
OTUB      rep1  A-X1
OTUA      rep1  EGG
OTUA      rep2  EGG
OTUA      rep1  EL
...       ...   ...    ...

我想在GEOM_BAR中绘制每个示例的ID,该示例的堆叠丰度对同类中的每个不同变量进行了堆叠。

graph <- ggplot(data, aes(x=ID, y=Abundance, fill=Class)) +
  facet_grid(~substrat, scales="free_x") +
  geom_bar(aes(color=Class, fill=Class), stat="identity", position="stack")

我得到这样的东西:

”在此处输入图像描述”

这很好,但是我想通过阶段变量订购X轴数据(ID),并带有特定顺序:“ A-X0”,“ Egg”,“ Egg”, “ el”,“ ll”,“ pp”,“ p”,“ a-x1”,但我不想拥有标签舞台,我想仍然拥有ID作为标签。

我已经尝试过:

graph$stage <- as.character(graph$stage)
graph$stage <- factor(x = graph$stage, levels = c("A-X0", "EGG", "EL", "LL","PP","P","A-X1"))

和:

test <- ggplot(data_ps.sam.env.rel, aes(x=reorder(ID, stage), y=Abundance, fill=Class)) +
  facet_grid(~substrat, scales="free_x") +
  geom_bar(aes(color=Class, fill=Class), stat="identity", position="stack") 

但是我仍然按随机顺序获得样本,相反,我希望所有具有“ J8”的ID都会在一起,所有具有“ J21”的人在一起,等等

I have a dataframe like follows:

Class       ID   Stage   Abundance          Substrat
OTUA      rep1  A-X1    123                  G
OTUA      rep2  A-X1    234                  PC
OTUA      rep3  A-X1    [numerical values]   [only two categorical values]
OTUB      rep1  A-X1
OTUA      rep1  EGG
OTUA      rep2  EGG
OTUA      rep1  EL
...       ...   ...    ...

I want to plot in geom_bar the ID of each sample by its stacked abundance for each different variable in the Class.

graph <- ggplot(data, aes(x=ID, y=Abundance, fill=Class)) +
  facet_grid(~substrat, scales="free_x") +
  geom_bar(aes(color=Class, fill=Class), stat="identity", position="stack")

I get something like this:

enter image description here

this works well, but I want to order the x axis data (ID) by the stage variable, with the specific order : "A-X0", "EGG", "EL", "LL","PP","P","A-X1", but I do not want to have the stage as label, I want to still have the ID as labels.

I have tried:

graph$stage <- as.character(graph$stage)
graph$stage <- factor(x = graph$stage, levels = c("A-X0", "EGG", "EL", "LL","PP","P","A-X1"))

and:

test <- ggplot(data_ps.sam.env.rel, aes(x=reorder(ID, stage), y=Abundance, fill=Class)) +
  facet_grid(~substrat, scales="free_x") +
  geom_bar(aes(color=Class, fill=Class), stat="identity", position="stack") 

but i still get the samples in a random order, instead I expect that all the ID with "J8" would be together, all those with "J21" together, etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2025-02-10 17:36:19

使用DPUT以您的数据示例可能是很棒的。
您如何考虑使用paste0(d $ stage,d $ id)使用此新变量在GGPLOT中添加新变量,然后使用REGEX在标签中删除D $级?

It could have been greate to have an example of your data using dput.
What do you think about adding a new variable using paste0(d$stage, d$ID) using this new variable in ggplot, and then removing the d$stage in the label using regex?

薆情海 2025-02-10 17:36:19

如果有人有兴趣,我解决了这个问题,
完成图后,我指定了因素的顺序:

graph$data$ID <- as.character(ps.1.sample_class$data$ID)
graph$data$ID <- factor(x = graph$data$ID, 
                        levels = unique(graph$data$ID[order(as.character(graph$data$stage))])) 

I resolved this issue if anyone is interested,
after doing the graph, I specified the order of the factors:

graph$data$ID <- as.character(ps.1.sample_class$data$ID)
graph$data$ID <- factor(x = graph$data$ID, 
                        levels = unique(graph$data$ID[order(as.character(graph$data$stage))])) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文