3

I have a function for creating a bar chart that looks like this:

my_bar_chart <- function(data, column, title, change_order=FALSE){
  out <-  data %>% 
    group_by({{column}}) %>% 
    summarize(count = n()) %>% 
    mutate(percent = count/sum(count),
           !! rlang::enquo(column) := if(change_order)
             reorder({{column}}, -count, FUN=identity) else {{column}} )
  ggplot(out, aes(x={{column}}, y=count, fill={{column}})) +
    xlab(title)+
    geom_col() +
    guides(fill=FALSE) + 
    geom_label(aes(label = paste0(round(100 * percent, 1), "%")))
}

my_bar_chart(d, audio_in_total, "EA5 Audio Inputs")

But my labels are coming out with their background color the same as the column, and hard to read I can't read them. enter image description here

I tried adding a white background like this, but it didn't work (it added a legend with the value of "white"):

geom_label(aes(label = paste0(round(100 * percent, 1), "%"), colour = "white"))

What is a better way to get the label background to be white?

My data has a column that looks like this:

audio_in_total
0
1
0
2
0
b-ryce
  • 5,752
  • 7
  • 49
  • 79
  • Color is the text & outline color, fill is the background. If you only want fill to affect the bars, you could put that `aes` argument just inside `geom_col`, rather than have it apply to all geoms – camille Jan 03 '20 at 20:34
  • Does this answer your question? [Set ggplot2 label background color](https://stackoverflow.com/questions/39514394/set-ggplot2-label-background-color) – camille Jan 03 '20 at 20:38

1 Answers1

4

Try to put the fill outside of the aes() call:

geom_label(aes(label = paste0(round(100 * percent, 1), "%")), fill = "white")
Dave Gruenewald
  • 5,329
  • 1
  • 23
  • 35
  • Thanks for the help! That changed the text to white, how do I change the background to white? – b-ryce Jan 03 '20 at 20:02