Search code examples
rggplot2labeljitter

r jitter stacked bar plot


I am plotting a stacked box plot to represent this data below

  Choice   Race   Freq countT per     
   <fct>    <ord> <dbl>  <dbl> <chr>   
 1 Pepsi    Asian   362  20684 1.7501% 
 2 Coke     Asian   363  20684 1.755%  
 3 Sprite   Asian   204  20684 0.9863% 
 4 Gatorade Asian     1  20684 0.0048% 
 5 Pepsi    Black   150  20684 0.7252% 
 6 Coke     Black   408  20684 1.9725% 
 7 Sprite   Black   322  20684 1.5568% 
 8 Gatorade Black    45  20684 0.2176% 
 9 Pepsi    Other  1088  20684 5.2601% 
10 Coke     Other  2407  20684 11.637% 
11 Sprite   Other   223  20684 1.0781% 
12 Gatorade Other   113  20684 0.5463% 
13 Pepsi    White  2384  20684 11.5258%
14 Coke     White  5038  20684 24.357% 
15 Sprite   White  6882  20684 33.2721%
16 Gatorade White   694  20684 3.3553% 

This is my code below

library(ggplot2)

colortest <- c("#FBCF357F", "#ED4C1C7F", "#9C7E707F", "#5AC2F17F" )

ggplot(testdf, aes(x = Choice, y = Freq, fill = Race, label = Freq)) +
  geom_bar(stat = "identity") + 
  geom_text(size = 4, position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values =  colortest) +
  scale_colour_manual(values = colortest) +
  coord_flip() +
  theme_bw() +
  ylab("Count by Race")

What I am seeing is a series of overlapping often hard to read labels like this.

enter image description here

How can i offset the position of these overlapping labels so that I see numbers without overlap ?

enter image description here

I have tried numerous other jitter options and all these failed.


Solution

  • Easy-cheesy with ggrepel. :-)

    enter image description here

    Code

    library(ggplot2)
    library(ggrepel)
    
    colortest <- c("#FBCF357F", "#ED4C1C7F", "#9C7E707F", "#5AC2F17F" )
    
    ggplot(df, aes(x = Choice, y = Freq, fill = Race, label = Freq)) +
      geom_bar(stat = "identity") + 
      # Here it is
      geom_text_repel(size = 4, position = position_stack(vjust = 0.5)) +
      scale_fill_manual(values =  colortest) +
      scale_colour_manual(values = colortest) +
      coord_flip() +
      theme_bw() +
      ylab("Count by Race")
    

    Data

    df <- read.table(text = "Choice   Race   Freq countT per      
     Pepsi    Asian   362  20684 1.7501% 
     Coke     Asian   363  20684 1.755%  
     Sprite   Asian   204  20684 0.9863% 
     Gatorade Asian     1  20684 0.0048% 
     Pepsi    Black   150  20684 0.7252% 
     Coke     Black   408  20684 1.9725% 
     Sprite   Black   322  20684 1.5568% 
     Gatorade Black    45  20684 0.2176% 
     Pepsi    Other  1088  20684 5.2601% 
    Coke     Other  2407  20684 11.637% 
    Sprite   Other   223  20684 1.0781% 
    Gatorade Other   113  20684 0.5463% 
    Pepsi    White  2384  20684 11.5258%
    Coke     White  5038  20684 24.357% 
    Sprite   White  6882  20684 33.2721%
    Gatorade White   694  20684 3.3553%", strip.white = TRUE, header = TRUE)