使用另一个分类变量订购X轴分类变量
我有一个数据框,如下所示:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用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?
如果有人有兴趣,我解决了这个问题,
完成图后,我指定了因素的顺序:
I resolved this issue if anyone is interested,
after doing the graph, I specified the order of the factors: