0

She wants to find out crimes in the USA for various states. So she plots the data set "USArrests" to construct a bar chart of the number of murders in different states.

Following command is giving error : ggplot(USArrests, aes(x = row.names(USArrests), y = USArrests$Murder, lab)) + geom_bar() + theme(axis.text.x=element_text(angle=90, hjust=1))

Error : "stat_count() must not be used with a y aesthetic". What can she do to correct this?

  1. Should I remove row.names — it should be x = USArrests$State ??
  2. Should I use theme(axis.angle.x = 90) instead of current one?
  3. Will geom_histogram() be better than Geom_bar?
  4. Or Should I use geom_col() instead of geom_bar()?

Please do suggest correct command. Thanks

1 Answers1

0

This is a very common issue, please see this. Just use geom_col instead of geom_bar, and also add row.names as a new variable to avoid further problems:

USArrests$states <- row.names(USArrests)

ggplot(USArrests, aes(x = states, y = Murder)) + 
  geom_col() + 
  theme(axis.text.x=element_text(angle=90, hjust=1))
Tito Sanz
  • 1,280
  • 1
  • 16
  • 33