Error: stat_bin() must not be used with a y aesthetic.

During the process of ggplot, an Error of “Error: stat_bin() must not be used with a y Aesthetic.”
code is as follows:

> ggplot(aes(x = gender, y = age),
+        data = subset(pf, !is.na(gender))) + geom_histogram()
Error: stat_bin() must not be used with a y aesthetic.
> ggplot(aes(x = age, y = friend_count),
+        data = subset(pf, !is.na(gender))) + geom_histogram()+
+   facet_wrap(~gender,ncol = 1)
Error: stat_bin() must not be used with a y aesthetic.

Stat_bin indicates that the statistical transformation is the count, and the count will be projected onto the Y-axis, which conflicts with y=1. Therefore, stat_bin() must not be used with a y aesthetic.
here is mainly because the histogram is a single-factor variable visualization method, and the Y-axis cannot be added in aes parcel. The correct code can be replaced as follows:

ggplot(aes(x = friend_count),
       data = subset(pf, !is.na(gender))) + geom_histogram()

ggplot(aes(x = friend_count),
       data = subset(pf, !is.na(gender))) + geom_histogram()+
  facet_wrap(~gender,ncol = 1)

 

Read More: