0

Edit: This question is not a duplicate of How to draw stacked bars in ggplot2 that show percentages based on group? . The data there is not purely factors and includes a "y =" in

ggplot(df, aes(x = factor(year), y = amount, fill = type))

As far as I know, "y =" is not relevant to my example.

That said, my goal is to display the percentages for the factor used in fill within ggplot. This is a bit different than other questions because I don't want to display the percentage for the x-variable—I want the percentages to be based on the fill variable, which will be a factor. It's probably easiest to illustrate with mtcars.

df<-data(mtcars)

ggplot(mtcars, aes(x = factor(mtcars$vs),fill=factor(mtcars$am))) +
  geom_bar(aes(y=(..count..)/sum(..count..))) +
  scale_y_continuous(labels=percent)

This gives us: ggplot of the above code

What I want, however, is something like this

Community
  • 1
  • 1
  • do you want the bars to be different heights based on number of observations? or just want to show the relative percentage in each bar? – Nate Apr 19 '17 at 21:10
  • @NateDay I want the bar height to be based on the x variable (in this case, the number of observations per factor in mtcars$vs). The text for the percentages, however, should total 100% per bar and show the percent of each mtcars$am per factor in mtcars$vs. – Will Wright Apr 19 '17 at 22:17
  • 1
    Possible duplicate of [How to draw stacked bars in ggplot2 that show percentages based on group?](http://stackoverflow.com/questions/22231124/how-to-draw-stacked-bars-in-ggplot2-that-show-percentages-based-on-group) – neilfws Apr 19 '17 at 22:20
  • @neilfws I've editted my question to clarify why I believe my situation is different. – Will Wright Apr 19 '17 at 22:56
  • I don't think your situation is different at all. You want percentages by group within stacked bars. It's the same problem as the duplicate question. – neilfws Apr 19 '17 at 23:05
  • @neilfws I'll keep working at it. Looks like the pre-processing requirement is where I'm getting hung up given that this is supposed to work within a larger function. – Will Wright Apr 19 '17 at 23:13
  • That error is probably because there is no `label` column in your data frame, as there is for the answer to the other question. There was another question only yesterday, very similar to yours, using the `mpg` data as an example; I'll try to find it. And there's this one too: http://stackoverflow.com/questions/43065409/stacked-bar-plot-label-bars-with-percentage-values – neilfws Apr 19 '17 at 23:16
  • 1
    @neilfws I was able to apply the technique in your link to mtcars and I believe this should work for my data as well. The key thing I was missing is that I need to do more data preprocessing in converting my factors to values that ggplot can understand. Thanks! – Will Wright Apr 20 '17 at 00:13
  • Adding labels to stacked bars is [here](http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2) – Engmdik Apr 20 '17 at 06:27

0 Answers0