wilkox / ggfittext

🔠 ggplot2 geoms to fit text into boxes
http://wilkox.org/ggfittext
305 stars 18 forks source link

Feature request: bar graphs #2

Closed sda030 closed 5 years ago

sda030 commented 6 years ago

Hi, nifty package. Would it be possible to implement a feature for the labels (typically percentages) that could be printed on top of a stacked bar graph? Or at least hide those which won't fit? I am currently using geom_col(position = "fill") + geom_text(aes(label = sprintf(prop*100, fmt="%1.0f%%")), position = position_stack(vjust = 0.5), size=3)

desc_impcur_topic_var_18

wilkox commented 6 years ago

Thanks @sda030. Stacked positions is definitely on the future additions list, as soon as I can figure out how to do it properly.

wilkox commented 6 years ago

@sda030 I've just pushed a dev version with support for position = "stack". To do this I had to change width and height from aesthetics to arguments, which is probably an improvement anyway. You'll still need to manually adjust the height/width to fit the bar. You can install the dev version with `devtools::install_github("wilkox/ggfittext").

breakdown <- data.frame(sector = rep("fast food", 4),
                        firm = c("McDonalds", "KFC", "Hungry Jacks", "Subway"),
                        share = c(40, 20, 8, 32))
ggplot(breakdown, aes(x = sector, y = share, fill = firm, label = firm)) +
  geom_bar(position = "stack", stat = "identity") +
  geom_fit_text(position = "stack", grow = T) +
  coord_flip()

screen shot 2017-12-13 at 12 22 18

Please give it a try and let me know how it works for you!

sda030 commented 6 years ago

Tried out a few ways and works well! (Sorry late reply)