【问题标题】:Adding values to geom_col messes up the whole plot向 geom_col 添加值会破坏整个情节
【发布时间】:2021-06-06 03:59:00
【问题描述】:

当我将 geom_text 添加到 geom_bar 时,整个情节变得一团糟。

    > x
# A tibble: 3 x 6
# Groups:   Jahr, Monat, Bez [3]
  Jahr  Bez                TWh Total Monat MonatsEAnteil
  <chr> <fct>            <dbl> <dbl> <chr>         <dbl>
1 2000  Kleinwasserkraft   0.2    12 Dec          0.0167
2 2000  Speicherkraft     20.3 22856 Jan          1.93  
3 2000  Speicherkraft     20.3 22856 Feb          1.30

 


 ggplot (data = x, aes(x=Monat, y=MonatsEAnteil, fill = MonatsEAnteil))+   
  • geom_col(aes(fill = Bez))
    

    > ggplot (data = x, aes(x=Monat, y=MonatsEAnteil, fill = factor(MonatsEAnteil)))+
    +     geom_col(aes(fill = Bez))+
    +     geom_text(aes (label = MonatsEAnteil), size = 3, 
position = position_stack(vjust = 0.5))

删除第二行 (geom_text) 使其再次正常。使用 geom_text 它创建一个图例(对于每个值?)。

【问题讨论】:

标签: r ggplot2 geom-bar geom-text


【解决方案1】:

找到了: 虽然它似乎适用于 geom_bar,但当填充等于 y 时,它不适用于 geom_test。相反,它应该从一开始就在 ggplot 中设置为“Bez”。删除(错误的)初始填充=因子(MonatsEAnteil)就可以了... 学习:

  1. 此处的填充美学必须是离散的,因为图例条目是 为每个价值而创造
  2. 原始“ggplot”中的一个错误(填充 = 连续变量而不是离散变量)以后无法纠正 在后续代码中

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 2019-10-26
    • 2014-09-10
    • 2020-12-11
    • 2017-11-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    相关资源
    最近更新 更多