代码之家  ›  专栏  ›  技术社区  ›  Joe

fill参数使geom_label()中的位置参数为空

  •  2
  • Joe  · 技术社区  · 7 年前

    图书馆(GG2)

    ds_mt <- 
      mtcars %>% 
      group_by(cyl, am) %>% 
      summarise(mpg = mean(mpg)) %>% 
      ungroup() %>% 
      mutate_at(vars(cyl, am), funs(as.factor)) 
    
    ds_mt %>% 
      ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
      geom_col(position = position_dodge()) + 
      geom_label(position = position_dodge(width = .9))
    
    # Adding fill argument to geom_label() nullifies the position argument
    ds_mt %>% 
      ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
      geom_col(position = position_dodge()) + 
      geom_label(fill = "white", position = position_dodge(width = .9))
    
    ds_mt %>% 
      ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
      geom_col(position = position_dodge()) + 
      geom_label(position = position_dodge(width = .9), fill = "white")
    
    # no problems with geom_text
    ds_mt %>% 
      ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
      geom_col(position = position_dodge()) + 
      geom_text(position = position_dodge(width = .9), color = "blue")
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Anonymous coward    7 年前

    您需要在数据库中定义一个组 geom_label()

    ds_mt %>% 
      ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
      geom_col(position = position_dodge()) + 
      geom_label(aes(group = factor(am)), fill = "white", position = position_dodge(width = .9))
    

    enter image description here