随着我已经得到了帮助,在这里就这样,我能建我想要的barplot。
现在我想使用与前一个非常相似的新数据来做完全相同的图,尽管它不起作用,我不知道为什么。
数据如下:
> dput(pcwdegs)
structure(list(comp = c("C0vsC12", "C0vsC12", "C0vsC24", "C0vsC24",
"C0vsT12", "C0vsT12", "C0vsT24", "C0vsT24", "C12vsC24", "C12vsC24",
"C12vsT12", "C12vsT12", "C12vsT24", "C12vsT24", "C24vsT12", "C24vsT12",
"C24vsT24", "C24vsT24", "T12vsT24", "T12vsT24"), reg = c("up",
"down", "up", "down", "up", "down", "up", "down", "up", "down",
"up", "down", "up", "down", "up", "down", "up", "down", "up",
"down"), count = c(7L, 15L, 11L, 5L, 51L, 50L, 49L, 38L, 10L,
2L, 54L, 44L, 55L, 32L, 24L, 32L, 12L, 17L, 31L, 22L)), class = "data.frame", row.names = c(NA,
-20L))
我收到的代码和警告消息(说所有观察结果都已删除)和情节:
ggplot(data = pcwdegs,
mapping = aes(x = comp, fill = reg,
y = ifelse(test = reg == "down",
yes = -count, no = count))) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = abs, limits = max(pcwdegs$count) * c(-1.05,1.05)) +
geom_text(aes(label=count), vjust=0.5, color="black", size=3.0, nudge_y = c(-1.5,1.5))+
labs(y = "DEGs related to plant cell wall", x = "Groups comparisons") +
scale_fill_manual(values=c("#98FB98","#FA8072","#00FF00","#FF0000"), name = "Expression regulation", labels = c("down-regulated", "up-regulated")) +
scale_x_discrete(limits=c("T12xT24", "C24xT24", "C24xT12", "C12xT24", "C12xT12", "C12xC24", "C0xT24", "C0xT12", "C0xC24","C0xC12")) +
coord_flip()
Warning messages:
1: Removed 20 rows containing missing values (position_stack).
2: Removed 20 rows containing missing values (geom_text).
3: Position guide is perpendicular to the intended axis. Did you mean to specify a different guide `position`?
在网上寻找解决方案,建议是检查是否有 NA 值(没有)并设置更高的限制limits = max(pcwdegs$count) * c(-1.05,1.05)
(因为它已经在我的第一个问题中提出并已完成)。所以我仍然不知道为什么这种类似的数据与代码的行为不同。
出现问题是因为新数据集中的类别不符合scale_x_discrete
. 新 df 中的类别在vs
中间有一个,而在限制中它是一个x
. 只需在限制中替换x
,vs
一切都很好。
library(ggplot2)
ggplot(data = pcwdegs,
mapping = aes(x = comp, fill = reg,
y = ifelse(test = reg == "down",
yes = -count, no = count))) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = abs, limits = max(pcwdegs$count) * c(-1.05,1.05)) +
geom_text(aes(label=count), vjust=0.5, color="black", size=3.0, nudge_y = c(-1.5,1.5))+
labs(y = "DEGs related to plant cell wall", x = "Groups comparisons") +
scale_fill_manual(values=c("#98FB98","#FA8072","#00FF00","#FF0000"), name = "Expression regulation", labels = c("down-regulated", "up-regulated")) +
#scale_x_discrete(limits=c("T12xT24", "C24xT24", "C24xT12", "C12xT24", "C12xT12", "C12xC24", "C0xT24", "C0xT12", "C0xC24","C0xC12")) +
scale_x_discrete(limits=c("T12vsT24", "C24vsT24", "C24vsT12", "C12vsT24", "C12vsT12", "C12vsC24", "C0vsT24", "C0vsT12", "C0vsC24","C0vsC12")) +
coord_flip()
由reprex 包(v0.3.0)于 2020 年 5 月 23 日创建
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句