在ggplot2中拆分条形图

时间:2018-03-22 01:37:25

标签: r

我是这里的超级新人,同时向前和向后工作从中间开始,所以请原谅我这是一个超级基本问题。

我有这段代码:

mh<-read.csv("mkhz_meta.csv")

msub<-mh[complete.cases(mh[ , 29]),]

msub2 <- subset(msub, Target.Fungal.Phylum!= "a")

png("J_Weighted_2_FungalPhylum.png", width= 800, height=600)
ggplot(aes(x = Target.Fungal.Phylum, fill= Concl..Weighted.nestedness), data =     msub2) + geom_bar(position = "dodge")+
xlab("Target Fungal Phylum")+ ylab("Count")+ ggtitle("Weighted vs. Primer  Specificity")+
theme_bw()+
scale_fill_grey()+
theme(axis.text.x =element_text(hjust = 0.5, size =8, angle= 0), 
    axis.text.y =element_text(size= 10), 
    legend.title=element_blank(), 
    legend.text=element_text(size=10), 
    axis.title.x =element_text(size=10),
    axis.title.y =element_text(size=10))+
theme(panel.grid.minor = element_blank(),
    panel.grid.major=element_blank(),
    strip.background = element_blank(),
    strip.text.x=element_text(size=14),
    panel.border = element_rect(colour = "black"))
 dev.off()

返回此图表:

Binary vs. Sequencing

有一个非嵌套的morphotyping计数,没有嵌套的morphotyping计数 - 我无法弄清楚如何进行“morphotyping”&#39;栏被拆分以反映这一点。任何帮助是极大的赞赏!

2 个答案:

答案 0 :(得分:1)

您可以使用tidyr::complete为数据添加缺失的计数。

示例数据:

mydata <- structure(list(platform = c("454", "454", "454", "454", "454", 
"454", "454", "454", "454", "454", "Morphotyping", "Sanger", 
"Sanger", "Sanger", "Sanger", "Sanger", "Sanger"), is_nested = c("nested", 
"not nested", "nested", "nested", "nested", "nested", "nested", 
"not nested", "nested", "not nested", "not nested", "nested", 
"nested", "nested", "not nested", "nested", "not nested")), .Names = 
c("platform", 
"is_nested"), row.names = c(NA, -17L), class = c("tbl_df", "tbl", 
"data.frame"), spec = structure(list(cols = structure(list(platform = 
structure(list(), class = c("collector_character", 
"collector")), is_nested = structure(list(), class = 
c("collector_character", 
"collector"))), .Names = c("platform", "is_nested")), default = 
structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

计算和绘图的代码:

library(tidyverse)
mydata %>% 
  count(platform, is_nested) %>% 
  complete(platform, is_nested) %>% 
  ggplot(aes(platform, n)) + 
    geom_col(aes(fill = is_nested), 
             position = position_dodge())

enter image description here

答案 1 :(得分:0)

尝试添加:

+ scale_fill_discrete(drop=FALSE) + scale_x_discrete(drop=FALSE)到你的情节命令来保存空的情节箱。