1

I'm trying to create a stacked bar chart that uses the values in 'MaskID' to create text labels. Each unique value in 'MaskID' will have its own color and I want the names of the values to be with their corresponding colors on the bar plot.

p <- ggplot(df, aes(x, y))
p <- p + xlab("xlabel")
p <- p + ylab("ylabel")
p <- p + ggtitle("ylabel vs xlabel")
p <- p + geom_bar(stat="identity", aes(fill=MaskID))
p <- p + geom_text(aes(label=MaskID))

I also don't want the names to show if the bar value is 0. The 'MaskID' names are all clustered together where the bar values are 0. Does anyone know how to customize this? I'm still quite new to ggplot2 (and R).

anonymous
  • 815
  • 3
  • 13
  • 21
  • try `p + geom_text(aes(label= ifelse(y == 0, '', MaskID)))` assuming `y` is the "bar value" _(Not tested)_ – Veerendra Gadekar Jul 27 '15 at 17:16
  • That helped partially. Thanks! It definitely got rid of the 0 value problem, but instead of putting the 'MaskID' names on the bar plot, it puts number values instead. – anonymous Jul 27 '15 at 17:23

1 Answers1

1

Try this

p + geom_text(aes(label= ifelse(y != 0, as.character(MaskID), '')))
Veerendra Gadekar
  • 4,452
  • 19
  • 24