rggplot2bar-chartmosaic-plot

remove duplicate labels in geom_text()


my dataset is something like:

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"), pricePerServing = c(5.6051, 0.8206, 4.38, 
8.1122, 1.505), healthScore = c(29, 4, 63, 70, 6), readyInMinutes = c(45L, 
15L, 45L, 45L, 15L), veryHealthy = c("False", "False", "True", 
"True", "False"), dairyFree = c("True", "True", "True", "True", 
"True"), dishType = c("lunch", "antipasti", "lunch", "lunch", 
"antipasti"), healthy = c(0.752433090024331, 0.752433090024331, 
0.247566909975669, 0.247566909975669, 0.752433090024331), diary = c(0.423965936739659, 
0.423965936739659, 0.423965936739659, 0.423965936739659, 0.423965936739659
), percent = c(0.370250606305578, 0.370250606305578, 0.587223587223587, 
0.587223587223587, 0.370250606305578)), row.names = c(NA, 5L), class = "data.frame")

my code is:


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

my first picture looks like this: enter image description here I want to make the labels on the middle of each bar so I use position = position_stack(0.5) and my code after modification is:

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

and then I get: enter image description here

my problem is not changing the height but it is those ugly black lines.

what should I do?


Solution

  • figured it out myself. it has been asked before in here but the answer is not right. if you have duplicates you should use stat = "unique"