ggplot2:关闭一层的指南

时间:2015-08-17 16:10:39

标签: r ggplot2

我试图在绘图中绘制两个图层 - 条形图和文本图层。条形图和文本图层由data.frame的互斥切片构成。切片由下面的分类变量Category的值定义。

现在,对于文本图层,我不希望显示指南。为了达到这个目的我:

  1. 使用droplevels确保Difference
  2. 中未使用冗余因子级别{geom_bar
  3. show_guide = FALSE
  4. geom_text

    但是,我仍然得到省略级别的省略级别的指南。

    这是一个演示问题的MWE:

    dfX = data_frame(
      id = rep.int(1:10, 3),
      Category = factor(rep(c("All", "Other", "Difference"), each = 10)), 
               Value = c(rpois(20, 10), abs(runif(10))*100))
    
    ggplot(data = droplevels(subset(dfX, !Category %in% "Difference")), 
           aes(x = as.factor(id), y = Value, fill = Category)) + 
      geom_bar(stat = "identity", position = "dodge") + 
      geom_text(data = droplevels(subset(dfX, Category %in% "Difference")), 
                aes(y = 10, label = signif(Value, 0)), 
                show_guide = FALSE) + 
      theme_bw() + 
      theme(legend.position = "bottom")
    

    enter image description here

    我不希望图例中突出显示的级别 - 我该如何实现?

1 个答案:

答案 0 :(得分:2)

您可以取消映射fill美学:

ggplot(data = droplevels(subset(dfX, !Category %in% "Difference")), 
             aes(x = as.factor(id), y = Value, fill = Category)) + 
    geom_bar(stat = "identity", position = "dodge") + 
    geom_text(data = droplevels(subset(dfX, Category %in% "Difference")), 
                        aes(y = 10, label = signif(Value, 0),fill = NULL), 
                        show_guide = FALSE) + 
    theme_bw() + 
    theme(legend.position = "bottom")
相关问题