-3

I am trying to plot proportions on the y-axis of my bar graph, as opposed to the usual count.

I am doing something like the following:

ggplot(data=mpg, aes(model))+geom_bar(aes(y=stat(count/sum(count)))

I am getting a blank plot.

Student
  • 107
  • 6
  • Dear Student, please make your question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Oct 07 '19 at 15:07
  • Why do you need `table`, can you show an image of your expected plot? – NelsonGon Oct 07 '19 at 15:14
  • Don't use `$` inside `aes` https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio – camille Oct 07 '19 at 15:18
  • @NelsonGon I'm changing my code accordingly. I got a partial answer here: https://stackoverflow.com/questions/36604127/creating-a-bar-plot-with-proportions-on-ggplot But it still isn't working. – Student Oct 07 '19 at 15:26
  • I don't get it: you had a reproducible example, and now you've edited it so you no longer do. Without having your data, it's difficult to do more than guess – camille Oct 07 '19 at 15:31
  • I apologise, have made it reproducible. – Student Oct 07 '19 at 15:59
  • You're missing a closing parenthesis at the end of that line. Once I add that, I get a plot that looks like what I'd expect. – camille Oct 07 '19 at 16:11
  • Can the downvoters explain their downvotes? – Student Oct 07 '19 at 16:29
  • Sure. I downvoted because after switching between reproducible and non-reproducible questions (which makes it hard for folks answering), what you've settled on seems like it might just be a missing parenthesis. There isn't evidence of much research here—even with the post you link to, you just said "it isn't working," which doesn't help folks help you debug. Take a look back through [ask] and the link in the 1st comment for advice on asking a question that is easy to answer and helpful for future site users – camille Oct 08 '19 at 03:25
  • I see. Thank you, I get that I should have researched more. – Student Oct 08 '19 at 06:24

2 Answers2

1

are you talking about coord_flip()? This will turn your chart 90 degrees

EDITED, Added below:

Try this below

  ggplot(data=mpg)+
    geom_bar(mapping=aes(x=model, y=..prop.., group=1))
Schilker
  • 505
  • 2
  • 11
  • No I am not. Basically, instead of count on the vertical axis, I want proportions. – Student Oct 07 '19 at 15:12
  • see my code in my above answer. "..prop.." will plot the proportions on the y-axis for your x-axis variables. Cheers! – Schilker Oct 07 '19 at 15:21
  • Excellent. Is there any way to show all percentages between 10 and 100, and to write them as percentages rather than decimals? – Student Oct 07 '19 at 16:13
  • Also, I don't understand the use of group. I've tried to look it up in RStudio, but to no avail. – Student Oct 08 '19 at 07:01
0

You need to create a data frame containing the proportions first, then use stat = "identity".

library(tidyverse)

mtcars %>% 
  as_tibble %>% 
  group_by(cyl) %>% 
  summarize(prop = n()/nrow(.)) %>% 
  ggplot() +
  geom_bar(aes(cyl, prop), stat = "identity")

shs
  • 3,683
  • 1
  • 6
  • 34
  • you don't need to create a whole new dataframe. Just change to "y=..prop..". See my answer. – Schilker Oct 07 '19 at 15:24
  • You are right, I didn't read your code after you talked about `coord_flip()`, which OP clearly did not ask for – shs Oct 07 '19 at 15:26
  • 1
    @Schilker you don't *need* to do the aggregation yourself beforehand, but it's often a good idea since it gives you better control than ggplot alone provides – camille Oct 07 '19 at 15:26
  • I agree @shs, less is more IMHO – Schilker Oct 07 '19 at 15:29