-2

When I add the geom_text to the geom_bar, the whole plot gets messed up.

    > 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))
    

enter image description here

    > 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))

enter image description here

removing the second line (geom_text) makes it OK again. With the geom_text it creates a legend (for every value?).

user202729
  • 3,358
  • 3
  • 25
  • 36
Beat
  • 1
  • 1

1 Answers1

0

Found it: Although it seems to work for geom_bar it does not work for geom_test when fill is equal to y. Rather it should be set to set to "Bez" from the outset in ggplot. Removing the (mistaken) initial fill = factor(MonatsEAnteil) does the trick... Learnings:

  1. The fill - aesthetic must be discrete here, since a legend entry is created for every value
  2. A mistake in the original "ggplot" (fill = continuous variable instead of discrete) cannot be rectified later in subsequent code
Beat
  • 1
  • 1