Home > OS >  How can I vertically centralize geom_text output on a stacked bar chart?
How can I vertically centralize geom_text output on a stacked bar chart?

Time:11-30

This is my dataset:

structure(list(title = c("Fried Anchovies with Sage", "Anchovies Appetizer With Breadcrumbs & Scallions", 
"Carrots, Cauliflower And Anchovies", "Bap Story: Stir Fried Anchovies (Myulchi Bokkeum)", 
"Fried Anchovies", "Tomato & Anchovies With Bread Crumbs, Basil & Red Onion   Recip", 
"Marinated Fresh Anchovies: Alici Marinate", "Marinated Boquerones", 
"Spaghetti With Walnuts And Anchovies", "Roasted Peppers with Boquerones"
), pricePerServing = c(5.6051, 0.8206, 4.38, 8.1122, 1.505, 0.8603, 
5.5005, 4.1332, 0.9284, 0.5844), healthScore = c(29, 4, 63, 70, 
6, 14, 33, 82, 45, 48), readyInMinutes = c(45L, 15L, 45L, 45L, 
15L, 45L, 420L, 45L, 12L, 4500L), veryHealthy = c("False", "False", 
"True", "True", "False", "False", "False", "True", "False", "False"
), dairyFree = c("True", "True", "True", "True", "True", "True", 
"True", "True", "False", "True"), dishType = c("lunch", "antipasti", 
"lunch", "lunch", "antipasti", "side dish", "lunch", "lunch", 
"lunch", "side dish"), healthy = c(0.752433090024331, 0.752433090024331, 
0.247566909975669, 0.247566909975669, 0.752433090024331, 0.752433090024331, 
0.752433090024331, 0.247566909975669, 0.752433090024331, 0.752433090024331
), diary = c(0.423965936739659, 0.423965936739659, 0.423965936739659, 
0.423965936739659, 0.423965936739659, 0.423965936739659, 0.423965936739659, 
0.423965936739659, 0.576034063260341, 0.423965936739659), percent = c(0.370250606305578, 
0.370250606305578, 0.587223587223587, 0.587223587223587, 0.370250606305578, 
0.370250606305578, 0.370250606305578, 0.587223587223587, 0.629749393694422, 
0.370250606305578)), row.names = c(NA, 10L), class = "data.frame")

I am trying to make a percent stacked barplot but I cannot vertically centralize my text. position_stack(vjust = 0.5) does not work. My code is:

  ggplot(foods, aes(x=veryHealthy, y=diary, width=healthy , fill = dairyFree,
                    label = percent))  
    geom_bar(stat='identity', position="fill")  
    scale_x_discrete(expand = c(0, 0))  
    facet_grid(~veryHealthy, scales = "free", space = "free")  
    geom_text(stat="unique", position = position_stack(vjust = 0.5))

I get this result: enter image description here How can I centralize the geom_text output here?

CodePudding user response:

I suggest to work with a summary table, so as your data provided is not complete, I created a small sample summary table to plot. Your task to get your data to a format like below.

foods <- data.frame(
  veryHealthy = c(F, F, T, T),
  dairyFree = c(T, F, T, F),
  percent = c(0.37, 0.63, 0.59, 0.41),
  healthy = c(0.75, 0.75, 0.25, 0.25)
)

ggplot(foods, aes(x = veryHealthy, y = percent, width = healthy, fill = dairyFree))  
  geom_bar(stat='identity', position = "fill")  
  geom_text(aes(label = percent), position = position_fill(vjust = 0.5))  
  scale_x_discrete(expand = c(0, 0))  
  facet_grid(~ veryHealthy, scales = "free", space = "free")

enter image description here

  • Related