从ggplot2中的facet_grid中删除未使用的因子

时间:2015-02-28 10:02:08

标签: r ggplot2

我有这个df:

    df <- data.frame(
      id = c(rep(1:3,20), rep(4:6,20)),
      type = rep(c("A","B","C","D"), each=3, times=10),
      timing = rep(c("T0","T1", "T2", "T3", "T4"), each=12, times=2),
      group = rep(c("Case","Control"),each=60),
      percentage = runif(120, min=15, max=25)
    )

scale="free"scale="free_y"无法制作此条形图:

  require(scales)
   ggplot(df, aes(x=id, y=percentage, fill=type)) +
   facet_grid(group ~ timing,scale="free") +
   geom_bar(position="stack",aes(width = 0.5),stat = "identity") +
   scale_y_continuous(labels = percent_format())+
   coord_flip()

enter image description here

0 个答案:

没有答案