0

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?

Hamed
  • 93
  • 5
  • To help us to help would you mind providing [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – stefan Nov 28 '21 at 12:32
  • 1
    ... this said. Removing color="white" makes the labels black. And the black lines are probably a lot of labels which are stacked on top of each other when switching to position = "stack". – stefan Nov 28 '21 at 12:34
  • @stefan yes, I am sorry I did not in the first place. hold on... – Hamed Nov 28 '21 at 13:16
  • @stefan but I think you are right, I have many labels. – Hamed Nov 28 '21 at 13:17
  • @stefan I changed it. – Hamed Nov 28 '21 at 14:14
  • @YBS I think the difference is I have 1640 rows but according to guidelines I gave 5 here – Hamed Nov 28 '21 at 15:26
  • @YBS if you look closely the title is repeated 5 times. – Hamed Nov 28 '21 at 15:27

1 Answers1

0

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"

Hamed
  • 93
  • 5