1

I created this bar chart using ggplot. I had to create my own count function and called it 'a,' but now the label for that axis just has an a and nothing else... How do I fix it?

a <- count(df, glass)

gl <- ggplot(df, aes(x=glass, y="a", fill=glass)) + 
    geom_bar(stat="identity") + 
    theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + 
    xlab("Glass type") + 
    ylab("Count") + 
    coord_flip() + 
    theme_minimal() + 
    theme(legend.position = "none") 
gl 

Here is my graph current result

MrFlick
  • 195,160
  • 17
  • 277
  • 295
a123nna
  • 13
  • 3
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 22 '20 at 22:35
  • If you want ggplot2 to count things up for you, trying using `geom_bar()` without using `y` at all and then remove `stat = "identity"` from the bar layer. – aosmith Jun 22 '20 at 22:40

1 Answers1

0

The default stat value for geom_bar is “count”, which means that geom_bar() uses stat_count() to count the rows of each x value, or glass in this case. Using stat="identity" will override the default geom_bar() stat and require that you provide the y values in order for aggregation to occur. I don't believe you are trying to do this.

Try the following and see if it is what you were looking for:

gl <- ggplot(df, aes(x=glass, fill=glass)) + 
        geom_bar() + 
        theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + 
        xlab("Glass type") + 
        ylab("Count") + 
        coord_flip() + 
        theme_minimal() + 
        theme(legend.position = "none")
Eric
  • 2,699
  • 5
  • 17