条形图中缺少值

时间:2020-05-23 19:19:18

标签: ggplot2

在我已经获得here的帮助下,我得以建立所需的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`?

enter image description here

在线寻找解决方案,建议是检查是否存在NA值(不存在),并在limits = max(pcwdegs$count) * c(-1.05,1.05)中设置更高的限制(正如我在第一个问题上已经建议并完成的那样)。因此,我仍然不知道为什么这些相似的数据与代码的行为不同。

1 个答案:

答案 0 :(得分:1)

出现问题是因为新数据集中的类别不符合def issubclass_(c, clazz): return isinstance(c, type) and issubclass(c, clazz) 定义的限制。新df中的类别中间有scale_x_discrete,而在限制中则是vs。只需将x替换为x即可,一切都很好。

vs

reprex package(v0.3.0)于2020-05-23创建