3

I have the following data:

df = data.frame(c("2012","2012","2012","2013"), c("tuesday","tuesday","friday","thursday"), c("AAA","BBB","AAA","AAA")) colnames(df) = c("year","day","type") 

I want to show the number of occurances (absolute frequency) of type values (AAA, BBB) per year and day. Currently I wrote the following code, but it requires that I add one more dimension to aes, e.g. aes(type, some_dimension, fill = as.factor(year)). So, how can I add something like count(type)?

ggplot(dat) + geom_bar(aes(type, fill = as.factor(year)), position = "dodge", stat = "identity") + facet_wrap(~day) 

1 Answer 1

5

In geom_bar change stat from "identity" to "count", like here:

library("ggplot2") ggplot(df) + geom_bar(aes(x = type, fill = as.factor(year)), position = "dodge", stat = "count") + facet_wrap(~day) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I get the error Error: stat_count() must not be used with a y aesthetic. on my real data. What does it mean and how to tackle it?
You probably added y to aes part (maybe not exactly, but as a second argument). Could you show your data and code? It would be easier then.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.