Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot table function objects with ggplot?

Tags:

I've got this data:

table(main$Sex1,main$District)

        Bahawalnagar Barkhan Chiniot  Faisalabad Ghotki 
Female    37           16       26       97         46          
Male      25           19       15       20         25 

I can plot it with base R

barplot(table(main$Sex1,main$District))

So my question is, how can I do this with ggplot2? Thanks

like image 794
Ali Zohaib Avatar asked Sep 22 '18 10:09

Ali Zohaib


1 Answers

ggplot(as.data.frame(table(main$Sex1,main$District)), aes(Var1, Freq, fill=Var2)) + 
  geom_bar(stat="identity")

table class is long, but it prints in a special way, and ggplot doesn't know how to handle it. If you pass it with as.data.frame it will be perfectly manageable by ggplot.

like image 169
iod Avatar answered Oct 12 '22 01:10

iod