Title: Analysis of Household Income and Expenditure

by Muhammad Salman Shah

Chapter 2

library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
mydata<-read.csv(file.choose())
mydata
##     Province Region  Income Expenditure
## 1     punjab  rural  140400      147596
## 2        kpk  urban   90000      201762
## 3   balochis  rural  356000      297956
## 4     punjab  urban  156000      144994
## 5        kpk  urban  282000      357188
## 6      sindh  rural  144000      159570
## 7        kpk  rural  248000      260594
## 8     punjab  rural   96000      105800
## 9      sindh  urban  205800      223508
## 10     sindh  rural   97200      111516
## 11     sindh  rural  225000      153132
## 12     sindh  rural  140000      143244
## 13  balochis  urban  108000      160854
## 14     sindh  rural  300000      252520
## 15    punjab  urban  840000      505670
## 16     sindh  rural  168000      185957
## 17    punjab  urban  120000      124792
## 18       kpk  rural  120000      104450
## 19     sindh  rural  120000      126920
## 20     sindh  urban  696000      635876
## 21    punjab  urban  355200      406982
## 22  balochis  urban  204000      222508
## 23    punjab  rural  168000      136252
## 24    punjab  urban  110000      190060
## 25    punjab  rural  347850      371358
## 26    punjab  rural  785000      257448
## 27       kpk  urban  387000      436252
## 28  balochis  rural  384000      412890
## 29     sindh  rural  114000      120922
## 30    punjab  rural  708000      730310
## 31     sindh  urban  252000      274720
## 32     sindh  rural  107000      119594
## 33       kpk  rural  216000      217326
## 34    punjab  urban  130000      141528
## 35  balochis  rural  360000      291650
## 36    punjab  rural  113400      186606
## 37     sindh  urban  180000      194978
## 38    punjab  urban  162900      184344
## 39    punjab  rural  320100      182656
## 40    punjab  rural  306000      412520
## 41    punjab  urban  422000      383792
## 42    punjab  urban  216000      245056
## 43    punjab  urban  432000      253670
## 44     sindh  rural   66840       69650
## 45  balochis  rural   78000       64710
## 46       kpk  urban  312000      331242
## 47    punjab  rural  190000      239750
## 48    punjab  rural  240550      225730
## 49  balochis  rural  396000      435854
## 50    punjab  rural 1485600      717746
## 51  balochis  urban  876000      871950
## 52       kpk  rural  224200      266207
## 53       kpk  rural  144000      155314
## 54  balochis  urban   60000       84384
## 55     sindh  rural  100000      116582
## 56     sindh  rural  288000      316324
## 57       kpk  rural   81000      215212
## 58    punjab  rural  265000      153674
## 59     sindh  rural  456000      379700
## 60    punjab  urban  194400      195520
## 61    punjab  rural   94500       80382
## 62  balochis  rural  185000      212480
## 63    punjab  rural  108000      122088
## 64    punjab  rural  243000      151628
## 65     sindh  urban  480000      581546
## 66    punjab  urban  346800      343620
## 67    punjab  rural  182000      142620
## 68    punjab  urban  240000      266950
## 69       kpk  urban  300000      277206
## 70     sindh  urban  720000      795430
## 71       kpk  urban  768000      529754
## 72     sindh  urban  318000      293584
## 73    punjab  rural  461750      144896
## 74  balochis  urban  720000      206700
## 75       kpk  rural  204000      152602
## 76     sindh  urban  534000      527306
## 77       kpk  urban  204000      197155
## 78     sindh  rural  107400      105366
## 79    punjab  urban  480000      405452
## 80    punjab  rural  420000      431536
## 81    punjab  urban  152400      177318
## 82       kpk  rural  222000      240798
## 83     sindh  urban  540000      507476
## 84    punjab  rural   24000       75442
## 85  balochis  rural  165000      189944
## 86    punjab  rural   14700       83120
## 87     sindh  rural  678000      642300
## 88    punjab  rural      NA      207056
## 89       kpk  rural  175000      301399
## 90    punjab  urban  144000      115818
## 91    punjab  urban  124800      102112
## 92  balochis  rural  168000      189422
## 93    punjab  rural   48000       55432
## 94     sindh  urban  264000      309326
## 95    punjab  rural   26400      144462
## 96    punjab  urban  138000      139582
## 97       kpk  rural  288000      319070
## 98  balochis  rural  150000      174004
## 99    punjab  urban  288000      247110
## 100   punjab  rural  100800      101848
## 101    sindh  rural  177000      198564
## 102    sindh  rural   65000      112924
## 103   punjab  rural   96000      111700
## 104   punjab  urban  315000      289386
## 105      kpk  rural  288000      405936
## 106    sindh  rural  336000      388940
## 107    sindh  rural   60500       64230
## 108    sindh  rural  660000      641710
## 109 balochis  rural   70000       87728
## 110    sindh  rural  204000      190298
## 111    sindh  rural  128300      145644
## 112   punjab  rural  167800      128668
## 113    sindh  rural  232400      192470
## 114   punjab  urban  426000      410800
## 115 balochis  urban  456000      262476
## 116    sindh  rural  247200      189652
## 117    sindh  rural  180000      169644
## 118    sindh  rural  345000      220048
## 119   punjab  urban  420000      329838
## 120    sindh  urban  264000      285542
## 121   punjab  rural   42000       75674
## 122 balochis  rural  250000      177714
## 123      kpk  urban  264000      313924
## 124   punjab  urban 1044000      553256
## 125    sindh  urban  660000      396156
## 126   punjab  rural   92000      106840
## 127    sindh  rural   81600       87504
## 128      kpk  rural  108000      193628
## 129      kpk  urban  288000      321940
## 130    sindh  rural  157500      140568
## 131    sindh  rural   60900       67968
## 132   punjab  urban  288000      317408
## 133      kpk  rural  334000      249706
## 134 balochis  rural  324000      267368
## 135      kpk  rural  600000      547977
## 136    sindh  rural  254000      211846
## 137   punjab  urban  192000      220248
## 138    sindh  rural  126000      139160
## 139      kpk  rural   20000      108050
## 140    sindh  rural  775000      324882
## 141    sindh  rural   77600       90050
## 142 balochis  rural  194800      141120
## 143    sindh  rural   48000       38720
## 144    sindh  rural  150000      197440
## 145    sindh  rural  153600      170410
## 146   punjab  rural  227500       95070
## 147    sindh  rural   90000       89116
## 148    sindh  rural  456000      477020
## 149    sindh  urban  286800      215100
## 150   punjab  rural  109050      214584
## 151      kpk  urban  252000      305914
## 152   punjab  rural  248000      223186
## 153   punjab  urban  280000      245220
## 154   punjab  rural   91300      186546
## 155   punjab  urban  240000      248508
## 156      kpk  rural   37500      130644
## 157   punjab  urban  528000      445268
## 158    sindh  rural   60000       65324
## 159      kpk  rural  105600      231576
## 160   punjab  urban   22800      113906
## 161    sindh  urban  276000      322362
## 162   punjab  rural  394000      445232
## 163    sindh  urban  246000      287770
## 164   punjab  urban  435600      472460
## 165   punjab  rural  255668      241870
## 166    sindh  rural  129000      124766
## 167   punjab  rural  165600      161056
## 168    sindh  rural   97000      111698
## 169   punjab  rural  230270      118084
## 170      kpk  urban   85050       89968
## 171      kpk  urban  234000      344800
## 172   punjab  rural  183700      188798
## 173   punjab  rural  187900      193591
## 174      kpk  rural  180000      194485
## 175   punjab  urban  513600      440572
## 176    sindh  urban  660000      558180
## 177   punjab  urban  108000      266270
## 178   punjab  urban  456000      412570
## 179   punjab  urban  268800      298722
## 180   punjab  rural  156000      145524
## 181    sindh  rural  180000      254968
## 182    sindh  rural   88200       78652
## 183    sindh  rural  499600      412390
## 184   punjab  urban 1020000      750980
## 185    sindh  rural  336200      204820
## 186      kpk  rural  336000      395670
## 187 balochis  rural  264000      272980
## 188   punjab  rural  779110      359890
## 189   punjab  urban  389000      427426
## 190    sindh  rural  157850      108250
## 191    sindh  rural  353000       58676
## 192      kpk  urban  173000      269813
## 193   punjab  rural  484000      382640
## 194      kpk  rural  272000      157085
## 195      kpk  urban  336000      393576
## 196   punjab  rural   62000       72742
## 197   punjab  urban  240000      281610
## 198    sindh  rural  102900      114046
## 199   punjab  rural  486030      208870
## 200   punjab  urban  236000      278744
## 201    sindh  urban  220000      195552
## 202 balochis  urban  138400      149800
## 203      kpk  rural  144000      118577
## 204   punjab  urban  300000      266940
## 205   punjab  rural  156000      164756
## 206   punjab  rural  168000      191532
## 207   punjab  urban  326400      335250
## 208    sindh  rural  120000      137702
## 209   punjab  rural   79200      347980
## 210   punjab  urban  168000      198012
## 211 balochis  rural  144000      168690
## 212      kpk  urban  192000      242376
## 213    sindh  urban  180000      160644
## 214   punjab  rural  200700      141330
## 215 balochis  rural  324000      306504
## 216   punjab  rural   36000       38206
## 217   punjab  rural  331750      195802
## 218    sindh  rural   76500       76000
## 219   punjab  rural   38000       96160
## 220   punjab  rural  354400      177500
## 221 balochis  rural  177000      188818
## 222   punjab  rural  200400      162402
## 223 balochis  rural  280000      342884
## 224   punjab  urban  258000      174348
## 225 balochis  rural  260000      301480
## 226   punjab  rural  133120      119070
## 227   punjab  rural   39500      155480
## 228   punjab  rural  228000       81930
## 229   punjab  rural   52000       67748
## 230 balochis  rural  144000      101808
## 231      kpk  urban  374400      475900
## 232    sindh  rural   66000       74314
## 233      kpk  rural  314000      310556
## 234      kpk  urban  420000      674720
## 235   punjab  rural   78800      116182
## 236      kpk  rural  216000      215958
## 237   punjab  urban  288000      270816
## 238   punjab  rural  857700      553650
## 239    sindh  rural  700000      584304
## 240 balochis  rural  168000      195210
## 241    sindh  urban  144000      135868
## 242   punjab  rural   30000       76306
## 243    sindh  rural  120000      112593
## 244      kpk  rural   11890      151688
## 245 balochis  rural  180000      208242
## 246   punjab  rural  141650      146506
## 247   punjab  rural  205000      232222
## 248      kpk  rural  205500      113924
## 249    sindh  urban  660000      881440
## 250   punjab  rural   25500       75172
## 251   punjab  rural   40500       41394
## 252   punjab  rural  432000      428478
## 253      kpk  rural  155500      191190
## 254    sindh  rural  216000      238440
## 255   punjab  rural   60000      198088
## 256    sindh  urban  360000      366230
## 257   punjab  rural  242200      248250
## 258   punjab  urban  504000      529376
## 259      kpk  rural  168000      265273
## 260   punjab  rural   67000       66330
## 261   punjab  rural  144000      110870
## 262    sindh  rural  124000      135114
## 263 balochis  rural   96000       88732
## 264   punjab  urban   60000       66818
## 265   punjab  rural  672000      537200
## 266      kpk  rural   88000      186414
## 267   punjab  rural  200400      183470
## 268    sindh  rural  393800      223300
## 269    sindh  urban  600000      457952
## 270   punjab  rural  122200      123749
## 271    sindh  urban  144000      151244
## 272 balochis  urban  240000      281076
## 273      kpk  rural  320000      342767
## 274   punjab  urban  210000      209752
## 275      kpk  rural  308000      276834
## 276   punjab  urban  186000      148322
str(mydata)
## 'data.frame':    276 obs. of  4 variables:
##  $ Province   : Factor w/ 4 levels "balochis","kpk",..: 3 2 1 3 2 4 2 3 4 4 ...
##  $ Region     : Factor w/ 2 levels "rural","urban": 1 2 1 2 2 1 1 1 2 1 ...
##  $ Income     : int  140400 90000 356000 156000 282000 144000 248000 96000 205800 97200 ...
##  $ Expenditure: int  147596 201762 297956 144994 357188 159570 260594 105800 223508 111516 ...
qplot(mydata$Income,mydata$Expenditure)
## Warning: Removed 1 rows containing missing values (geom_point).

qplot(mydata$Income,mydata$Expenditure)
## Warning: Removed 1 rows containing missing values (geom_point).

qplot(Income,Expenditure,data = mydata)
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

plot(mydata$Income, mydata$Expenditure, type="l")
points(mydata$Income, mydata$Expenditure)

plot(mydata$Income, mydata$Expenditure/2, col="red")
points(mydata$Income, mydata$Expenditure/2,col="red")

qplot(mydata$Income,mydata$Expenditure,geom = "line")
## Warning: Removed 1 rows containing missing values (geom_path).

qplot(Income,Expenditure,data = mydata,geom = "line")
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()
## Warning: Removed 1 rows containing missing values (geom_path).

qplot(Income,Expenditure,data = mydata,geom=c("line","point"))
## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()+geom_point()
## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_point).

barplot(mydata$Income, names.arg=mydata$Expenditure)

table(mydata$Income)
## 
##   11890   14700   20000   22800   24000   25500   26400   30000   36000 
##       1       1       1       1       1       1       1       1       1 
##   37500   38000   39500   40500   42000   48000   52000   60000   60500 
##       1       1       1       1       1       2       1       4       1 
##   60900   62000   65000   66000   66840   67000   70000   76500   77600 
##       1       1       1       1       1       1       1       1       1 
##   78000   78800   79200   81000   81600   85050   88000   88200   90000 
##       1       1       1       1       1       1       1       1       2 
##   91300   92000   94500   96000   97000   97200  100000  100800  102900 
##       1       1       1       3       1       1       1       1       1 
##  105600  107000  107400  108000  109050  110000  113400  114000  120000 
##       1       1       1       4       1       1       1       1       5 
##  122200  124000  124800  126000  128300  129000  130000  133120  138000 
##       1       1       1       1       1       1       1       1       1 
##  138400  140000  140400  141650  144000  150000  152400  153600  155500 
##       1       1       1       1       9       2       1       1       1 
##  156000  157500  157850  162900  165000  165600  167800  168000  173000 
##       3       1       1       1       1       1       1       7       1 
##  175000  177000  180000  182000  183700  185000  186000  187900  190000 
##       1       2       6       1       1       1       1       1       1 
##  192000  194400  194800  200400  200700  204000  205000  205500  205800 
##       2       1       1       2       1       4       1       1       1 
##  210000  216000  220000  222000  224200  225000  227500  228000  230270 
##       1       4       1       1       1       1       1       1       1 
##  232400  234000  236000  240000  240550  242200  243000  246000  247200 
##       1       1       1       4       1       1       1       1       1 
##  248000  250000  252000  254000  255668  258000  260000  264000  265000 
##       2       1       2       1       1       1       1       4       1 
##  268800  272000  276000  280000  282000  286800  288000  300000  306000 
##       1       1       1       2       1       1       7       3       1 
##  308000  312000  314000  315000  318000  320000  320100  324000  326400 
##       1       1       1       1       1       1       1       2       1 
##  331750  334000  336000  336200  345000  346800  347850  353000  354400 
##       1       1       3       1       1       1       1       1       1 
##  355200  356000  360000  374400  384000  387000  389000  393800  394000 
##       1       1       2       1       1       1       1       1       1 
##  396000  420000  422000  426000  432000  435600  456000  461750  480000 
##       1       3       1       1       2       1       4       1       2 
##  484000  486030  499600  504000  513600  528000  534000  540000  600000 
##       1       1       1       1       1       1       1       1       2 
##  660000  672000  678000  696000  700000  708000  720000  768000  775000 
##       4       1       1       1       1       1       2       1       1 
##  779110  785000  840000  857700  876000 1020000 1044000 1485600 
##       1       1       1       1       1       1       1       1
barplot(table(mydata$Income))

qplot(factor(Income),data = mydata)

ggplot(mydata,aes(factor(Income)))+geom_bar()

hist(mydata$Income)

hist(mydata$Expenditure)

hist(mydata$Income,breaks = 10)

qplot(mydata$Income)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (stat_bin).

plot(mydata$Income,mydata$Expenditure)

qplot(mydata$Province,mydata$Income,geom = "boxplot")
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

qplot(Province,Income,data = mydata,geom = "boxplot")
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata,aes(x=Province,y=Income))+geom_boxplot()
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

qplot(interaction(mydata$Province, mydata$Region), mydata$Income,geom="boxplot")
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

qplot(interaction(Province,Region),Income,data = mydata,geom = "boxplot")
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata,aes(x=interaction(Province,Region),Income))+geom_boxplot()
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

#chapter#3

ggplot(mydata,aes(x=Province))+geom_bar()

ggplot(mydata,aes(x=Province,y=Income,fill=Region))+geom_bar(stat = "identity")
## Warning: Removed 1 rows containing missing values (position_stack).

ggplot(mydata,aes(x=reorder(Province,Income),y=Income,fill=Region))+geom_bar(stat = "identity")+scale_fill_manual(values = c("#669933", "#FFCC66"))+xlab("Province")
## Warning: Removed 1 rows containing missing values (position_stack).

ggplot(mydata,aes(x=Province,y=Expenditure,fill=Region))+geom_bar(stat = "identity",position = "identity")

ggplot(mydata,aes(x=Province,y=Income))+geom_bar(stat = "identity",width = 1)
## Warning: Removed 1 rows containing missing values (position_stack).

ggplot(mydata,aes(x=Province,y=Income,fill=Region))+geom_bar(stat = "identity",width = 0.5,position = "dodge")
## Warning: Removed 1 rows containing missing values (geom_bar).

ggplot(mydata,aes(x=Province,y=Income,fill=Region))+geom_bar(stat = "identity",width = 0.5,position = position_dodge(0.7))
## Warning: Removed 1 rows containing missing values (geom_bar).

ggplot(mydata,aes(x=Province,y=Expenditure,fill=Region))+geom_bar(stat = "identity")

ggplot(mydata,aes(x=Province,y=Expenditure,fill=Region))+geom_bar(stat = "identity")+guides(fill=guide_legend(reverse = TRUE))

ggplot(mydata,aes(x=Province,y=Expenditure,fill=Region))+geom_bar(stat = "identity",colour="black")+guides(fill=guide_legend(reverse = TRUE))+scale_fill_brewer(palette = "Pastel1")

mydata
##     Province Region  Income Expenditure
## 1     punjab  rural  140400      147596
## 2        kpk  urban   90000      201762
## 3   balochis  rural  356000      297956
## 4     punjab  urban  156000      144994
## 5        kpk  urban  282000      357188
## 6      sindh  rural  144000      159570
## 7        kpk  rural  248000      260594
## 8     punjab  rural   96000      105800
## 9      sindh  urban  205800      223508
## 10     sindh  rural   97200      111516
## 11     sindh  rural  225000      153132
## 12     sindh  rural  140000      143244
## 13  balochis  urban  108000      160854
## 14     sindh  rural  300000      252520
## 15    punjab  urban  840000      505670
## 16     sindh  rural  168000      185957
## 17    punjab  urban  120000      124792
## 18       kpk  rural  120000      104450
## 19     sindh  rural  120000      126920
## 20     sindh  urban  696000      635876
## 21    punjab  urban  355200      406982
## 22  balochis  urban  204000      222508
## 23    punjab  rural  168000      136252
## 24    punjab  urban  110000      190060
## 25    punjab  rural  347850      371358
## 26    punjab  rural  785000      257448
## 27       kpk  urban  387000      436252
## 28  balochis  rural  384000      412890
## 29     sindh  rural  114000      120922
## 30    punjab  rural  708000      730310
## 31     sindh  urban  252000      274720
## 32     sindh  rural  107000      119594
## 33       kpk  rural  216000      217326
## 34    punjab  urban  130000      141528
## 35  balochis  rural  360000      291650
## 36    punjab  rural  113400      186606
## 37     sindh  urban  180000      194978
## 38    punjab  urban  162900      184344
## 39    punjab  rural  320100      182656
## 40    punjab  rural  306000      412520
## 41    punjab  urban  422000      383792
## 42    punjab  urban  216000      245056
## 43    punjab  urban  432000      253670
## 44     sindh  rural   66840       69650
## 45  balochis  rural   78000       64710
## 46       kpk  urban  312000      331242
## 47    punjab  rural  190000      239750
## 48    punjab  rural  240550      225730
## 49  balochis  rural  396000      435854
## 50    punjab  rural 1485600      717746
## 51  balochis  urban  876000      871950
## 52       kpk  rural  224200      266207
## 53       kpk  rural  144000      155314
## 54  balochis  urban   60000       84384
## 55     sindh  rural  100000      116582
## 56     sindh  rural  288000      316324
## 57       kpk  rural   81000      215212
## 58    punjab  rural  265000      153674
## 59     sindh  rural  456000      379700
## 60    punjab  urban  194400      195520
## 61    punjab  rural   94500       80382
## 62  balochis  rural  185000      212480
## 63    punjab  rural  108000      122088
## 64    punjab  rural  243000      151628
## 65     sindh  urban  480000      581546
## 66    punjab  urban  346800      343620
## 67    punjab  rural  182000      142620
## 68    punjab  urban  240000      266950
## 69       kpk  urban  300000      277206
## 70     sindh  urban  720000      795430
## 71       kpk  urban  768000      529754
## 72     sindh  urban  318000      293584
## 73    punjab  rural  461750      144896
## 74  balochis  urban  720000      206700
## 75       kpk  rural  204000      152602
## 76     sindh  urban  534000      527306
## 77       kpk  urban  204000      197155
## 78     sindh  rural  107400      105366
## 79    punjab  urban  480000      405452
## 80    punjab  rural  420000      431536
## 81    punjab  urban  152400      177318
## 82       kpk  rural  222000      240798
## 83     sindh  urban  540000      507476
## 84    punjab  rural   24000       75442
## 85  balochis  rural  165000      189944
## 86    punjab  rural   14700       83120
## 87     sindh  rural  678000      642300
## 88    punjab  rural      NA      207056
## 89       kpk  rural  175000      301399
## 90    punjab  urban  144000      115818
## 91    punjab  urban  124800      102112
## 92  balochis  rural  168000      189422
## 93    punjab  rural   48000       55432
## 94     sindh  urban  264000      309326
## 95    punjab  rural   26400      144462
## 96    punjab  urban  138000      139582
## 97       kpk  rural  288000      319070
## 98  balochis  rural  150000      174004
## 99    punjab  urban  288000      247110
## 100   punjab  rural  100800      101848
## 101    sindh  rural  177000      198564
## 102    sindh  rural   65000      112924
## 103   punjab  rural   96000      111700
## 104   punjab  urban  315000      289386
## 105      kpk  rural  288000      405936
## 106    sindh  rural  336000      388940
## 107    sindh  rural   60500       64230
## 108    sindh  rural  660000      641710
## 109 balochis  rural   70000       87728
## 110    sindh  rural  204000      190298
## 111    sindh  rural  128300      145644
## 112   punjab  rural  167800      128668
## 113    sindh  rural  232400      192470
## 114   punjab  urban  426000      410800
## 115 balochis  urban  456000      262476
## 116    sindh  rural  247200      189652
## 117    sindh  rural  180000      169644
## 118    sindh  rural  345000      220048
## 119   punjab  urban  420000      329838
## 120    sindh  urban  264000      285542
## 121   punjab  rural   42000       75674
## 122 balochis  rural  250000      177714
## 123      kpk  urban  264000      313924
## 124   punjab  urban 1044000      553256
## 125    sindh  urban  660000      396156
## 126   punjab  rural   92000      106840
## 127    sindh  rural   81600       87504
## 128      kpk  rural  108000      193628
## 129      kpk  urban  288000      321940
## 130    sindh  rural  157500      140568
## 131    sindh  rural   60900       67968
## 132   punjab  urban  288000      317408
## 133      kpk  rural  334000      249706
## 134 balochis  rural  324000      267368
## 135      kpk  rural  600000      547977
## 136    sindh  rural  254000      211846
## 137   punjab  urban  192000      220248
## 138    sindh  rural  126000      139160
## 139      kpk  rural   20000      108050
## 140    sindh  rural  775000      324882
## 141    sindh  rural   77600       90050
## 142 balochis  rural  194800      141120
## 143    sindh  rural   48000       38720
## 144    sindh  rural  150000      197440
## 145    sindh  rural  153600      170410
## 146   punjab  rural  227500       95070
## 147    sindh  rural   90000       89116
## 148    sindh  rural  456000      477020
## 149    sindh  urban  286800      215100
## 150   punjab  rural  109050      214584
## 151      kpk  urban  252000      305914
## 152   punjab  rural  248000      223186
## 153   punjab  urban  280000      245220
## 154   punjab  rural   91300      186546
## 155   punjab  urban  240000      248508
## 156      kpk  rural   37500      130644
## 157   punjab  urban  528000      445268
## 158    sindh  rural   60000       65324
## 159      kpk  rural  105600      231576
## 160   punjab  urban   22800      113906
## 161    sindh  urban  276000      322362
## 162   punjab  rural  394000      445232
## 163    sindh  urban  246000      287770
## 164   punjab  urban  435600      472460
## 165   punjab  rural  255668      241870
## 166    sindh  rural  129000      124766
## 167   punjab  rural  165600      161056
## 168    sindh  rural   97000      111698
## 169   punjab  rural  230270      118084
## 170      kpk  urban   85050       89968
## 171      kpk  urban  234000      344800
## 172   punjab  rural  183700      188798
## 173   punjab  rural  187900      193591
## 174      kpk  rural  180000      194485
## 175   punjab  urban  513600      440572
## 176    sindh  urban  660000      558180
## 177   punjab  urban  108000      266270
## 178   punjab  urban  456000      412570
## 179   punjab  urban  268800      298722
## 180   punjab  rural  156000      145524
## 181    sindh  rural  180000      254968
## 182    sindh  rural   88200       78652
## 183    sindh  rural  499600      412390
## 184   punjab  urban 1020000      750980
## 185    sindh  rural  336200      204820
## 186      kpk  rural  336000      395670
## 187 balochis  rural  264000      272980
## 188   punjab  rural  779110      359890
## 189   punjab  urban  389000      427426
## 190    sindh  rural  157850      108250
## 191    sindh  rural  353000       58676
## 192      kpk  urban  173000      269813
## 193   punjab  rural  484000      382640
## 194      kpk  rural  272000      157085
## 195      kpk  urban  336000      393576
## 196   punjab  rural   62000       72742
## 197   punjab  urban  240000      281610
## 198    sindh  rural  102900      114046
## 199   punjab  rural  486030      208870
## 200   punjab  urban  236000      278744
## 201    sindh  urban  220000      195552
## 202 balochis  urban  138400      149800
## 203      kpk  rural  144000      118577
## 204   punjab  urban  300000      266940
## 205   punjab  rural  156000      164756
## 206   punjab  rural  168000      191532
## 207   punjab  urban  326400      335250
## 208    sindh  rural  120000      137702
## 209   punjab  rural   79200      347980
## 210   punjab  urban  168000      198012
## 211 balochis  rural  144000      168690
## 212      kpk  urban  192000      242376
## 213    sindh  urban  180000      160644
## 214   punjab  rural  200700      141330
## 215 balochis  rural  324000      306504
## 216   punjab  rural   36000       38206
## 217   punjab  rural  331750      195802
## 218    sindh  rural   76500       76000
## 219   punjab  rural   38000       96160
## 220   punjab  rural  354400      177500
## 221 balochis  rural  177000      188818
## 222   punjab  rural  200400      162402
## 223 balochis  rural  280000      342884
## 224   punjab  urban  258000      174348
## 225 balochis  rural  260000      301480
## 226   punjab  rural  133120      119070
## 227   punjab  rural   39500      155480
## 228   punjab  rural  228000       81930
## 229   punjab  rural   52000       67748
## 230 balochis  rural  144000      101808
## 231      kpk  urban  374400      475900
## 232    sindh  rural   66000       74314
## 233      kpk  rural  314000      310556
## 234      kpk  urban  420000      674720
## 235   punjab  rural   78800      116182
## 236      kpk  rural  216000      215958
## 237   punjab  urban  288000      270816
## 238   punjab  rural  857700      553650
## 239    sindh  rural  700000      584304
## 240 balochis  rural  168000      195210
## 241    sindh  urban  144000      135868
## 242   punjab  rural   30000       76306
## 243    sindh  rural  120000      112593
## 244      kpk  rural   11890      151688
## 245 balochis  rural  180000      208242
## 246   punjab  rural  141650      146506
## 247   punjab  rural  205000      232222
## 248      kpk  rural  205500      113924
## 249    sindh  urban  660000      881440
## 250   punjab  rural   25500       75172
## 251   punjab  rural   40500       41394
## 252   punjab  rural  432000      428478
## 253      kpk  rural  155500      191190
## 254    sindh  rural  216000      238440
## 255   punjab  rural   60000      198088
## 256    sindh  urban  360000      366230
## 257   punjab  rural  242200      248250
## 258   punjab  urban  504000      529376
## 259      kpk  rural  168000      265273
## 260   punjab  rural   67000       66330
## 261   punjab  rural  144000      110870
## 262    sindh  rural  124000      135114
## 263 balochis  rural   96000       88732
## 264   punjab  urban   60000       66818
## 265   punjab  rural  672000      537200
## 266      kpk  rural   88000      186414
## 267   punjab  rural  200400      183470
## 268    sindh  rural  393800      223300
## 269    sindh  urban  600000      457952
## 270   punjab  rural  122200      123749
## 271    sindh  urban  144000      151244
## 272 balochis  urban  240000      281076
## 273      kpk  rural  320000      342767
## 274   punjab  urban  210000      209752
## 275      kpk  rural  308000      276834
## 276   punjab  urban  186000      148322
ggplot(mydata,aes(x=interaction(Province,Region),y=Income))+geom_bar(stat = "identity")+geom_text(aes(label=Income),vjust=-0.2)
## Warning: Removed 1 rows containing missing values (position_stack).
## Warning: Removed 1 rows containing missing values (geom_text).

ggplot(mydata,aes(x=interaction(Province,Region),y=Income))+geom_bar(stat = "identity")+geom_text(aes(label=Income),vjust=-0.2)+ylim(0,max(mydata$Income)*1.05)
## Warning: Removed 1 rows containing missing values (position_stack).

## Warning: Removed 1 rows containing missing values (geom_text).

ggplot(mydata,aes(x=interaction(Province,Region),y=Income))+geom_bar(stat = "identity")+geom_text(aes(y=Income+0.1,label=Income))
## Warning: Removed 1 rows containing missing values (position_stack).

## Warning: Removed 1 rows containing missing values (geom_text).

ggplot(mydata,aes(x=Province,y=Income,fill=Region))+geom_bar(stat = "identity",position = "dodge")+geom_text(aes(label=Income),vjust=1.5,colour="white",position = position_dodge(0.9),size=3)
## Warning: Removed 1 rows containing missing values (geom_bar).

## Warning: Removed 1 rows containing missing values (geom_text).

ggplot(mydata,aes(x=Income,y=Province))+geom_point()                                                                                                      
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=reorder(Province,Income)))+geom_point(size=3)+theme_bw()+theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(),panel.grid.major.y = element_line(colour = "grey60",linetype = "dashed"))
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=reorder(Province,Income),y=Income))+geom_point(size=3)+theme_bw()+theme(axis.text.x = element_text(angle=60,hjust=1),panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank(),panel.grid.major.x = element_line(colour = "grey60",linetype = "dashed"))
## Warning: Removed 1 rows containing missing values (geom_point).

#chapter#4 Line graph

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()
## Warning: Removed 1 rows containing missing values (geom_path).

mydata1<-mydata
mydata1$Income<-factor(mydata1$Income)
ggplot(mydata1,aes(x=Income,y=Expenditure,group=1))+geom_line()

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()+ylim(0,max(mydata$Expenditure))
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()+expand_limits(y=0)
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()+geom_point()
## Warning: Removed 1 rows containing missing values (geom_path).
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()+geom_point()+scale_y_log10()
## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_point).

library(plyr)
mydata$Region
##   [1] rural urban rural urban urban rural rural rural urban rural rural
##  [12] rural urban rural urban rural urban rural rural urban urban urban
##  [23] rural urban rural rural urban rural rural rural urban rural rural
##  [34] urban rural rural urban urban rural rural urban urban urban rural
##  [45] rural urban rural rural rural rural urban rural rural urban rural
##  [56] rural rural rural rural urban rural rural rural rural urban urban
##  [67] rural urban urban urban urban urban rural urban rural urban urban
##  [78] rural urban rural urban rural urban rural rural rural rural rural
##  [89] rural urban urban rural rural urban rural urban rural rural urban
## [100] rural rural rural rural urban rural rural rural rural rural rural
## [111] rural rural rural urban urban rural rural rural urban urban rural
## [122] rural urban urban urban rural rural rural urban rural rural urban
## [133] rural rural rural rural urban rural rural rural rural rural rural
## [144] rural rural rural rural rural urban rural urban rural urban rural
## [155] urban rural urban rural rural urban urban rural urban urban rural
## [166] rural rural rural rural urban urban rural rural rural urban urban
## [177] urban urban urban rural rural rural rural urban rural rural rural
## [188] rural urban rural rural urban rural rural urban rural urban rural
## [199] rural urban urban urban rural urban rural rural urban rural rural
## [210] urban rural urban urban rural rural rural rural rural rural rural
## [221] rural rural rural urban rural rural rural rural rural rural urban
## [232] rural rural urban rural rural urban rural rural rural urban rural
## [243] rural rural rural rural rural rural urban rural rural rural rural
## [254] rural rural urban rural urban rural rural rural rural rural urban
## [265] rural rural rural rural urban rural urban urban rural urban rural
## [276] urban
## Levels: rural urban
tg<-ddply(mydata,c("Region","Income"),summarise,length=mean(Expenditure))
ggplot(tg,aes(x=Income,y=length,colour=Region))+geom_line()          
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(tg,aes(x=Income,y=length,linetype=Region))+geom_line()
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(tg,aes(x=factor(Income),y=length,colour=Region,group=Region))+geom_line()

ggplot(tg,aes(x=Income,y=length))+geom_line()
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(tg,aes(x=Income,y=length,shape=Region))+geom_line()+geom_point(size=4)
## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(tg,aes(x=Income,y=length,fill=Region))+geom_line()+geom_point(size=4,shape=21)
## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(tg,aes(x=Income,y=length,shape=Region))+geom_line(position = position_dodge(0.2))+geom_point(position = position_dodge(0.2),size=2)
## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line(linetype="dashed",size=1,colour="blue")
## Warning: Removed 1 rows containing missing values (geom_path).

tg<-ddply(mydata,c("Region","Income"),summarise,length=mean(Expenditure))
ggplot(tg,aes(x=Income,y=length,colour=Region))+geom_line()+scale_color_brewer(palette = "set1")
## Warning in pal_name(palette, type): Unknown palette set1
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(tg,aes(x=Income,y=length,group=Region))+geom_line(colour="darkgreen",size=1.5)
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(tg,aes(x=Income,y=length,colour=Region))+geom_line(linetype="dashed")+geom_point(shape=22,size=3,fill="white") 
## Warning: Removed 1 rows containing missing values (geom_path).
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()+geom_point(size=4,shape=22,colour="darkblue",fill="pink")
## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_line()+geom_point(size=4,shape=21,colour="white")
## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_point).

tg<-ddply(mydata,c("Region","Income"),summarise,length=mean(Expenditure))
pd<-position_dodge(0.2)
ggplot(tg,aes(x=Income,y=length,fill=Region))+geom_line(position = pd)+geom_point(shape=21,size=3,position = pd)+scale_fill_manual(values = c("black","white"))
## Warning: Removed 1 rows containing missing values (geom_path).

## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_area()
## Warning: Removed 1 rows containing missing values (position_stack).

ggplot(mydata,aes(x=Income,y=Expenditure,fill=Province))+geom_area()
## Warning: Removed 1 rows containing missing values (position_stack).

mydata1<-ddply(mydata,"Income",transform,percent=Expenditure/sum(Expenditure)*100)
ggplot(mydata1,aes(x=Income,y=percent,fill=Province))+geom_area(colour="black",size=0.2,alpha=0.4)+scale_fill_brewer(palette = "Blues",breaks=rev(mydata$Province))
## Warning: Removed 1 rows containing missing values (position_stack).

#Chapter#5

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_point(shape=21)
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_point(size=2)
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure,colour=Region))+geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure,shape=Region))+geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure,shape=Region,colour=Region))+geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure,shape=Region,colour=Region))+geom_point()+scale_shape_manual(values = c(1,2))+scale_colour_brewer(palette="Set1")
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_point(shape=3)
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure,shape=Region))+geom_point(size=3)+scale_shape_manual(values=c(1,4))
## Warning: Removed 1 rows containing missing values (geom_point).

sp<-ggplot(mydata,aes(x=Income,y=Expenditure))
sp+geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

sp+geom_point(alpha=.1)
## Warning: Removed 1 rows containing missing values (geom_point).

sp+geom_point(alpha=.5)
## Warning: Removed 1 rows containing missing values (geom_point).

sp1<-ggplot(mydata,aes(x=Province,y=Income))
sp1+geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).

sp1 + geom_point(position=position_jitter(width=.5, height=0))
## Warning: Removed 1 rows containing missing values (geom_point).

sp1+geom_boxplot(aes(group=Region))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

sp<-ggplot(mydata,aes(x=Income,y=Expenditure))
sp + geom_point() + stat_smooth(method=lm)
## Warning: Removed 1 rows containing non-finite values (stat_smooth).

## Warning: Removed 1 rows containing missing values (geom_point).

sp + geom_point() + stat_smooth(method=lm,level = 0.99)
## Warning: Removed 1 rows containing non-finite values (stat_smooth).

## Warning: Removed 1 rows containing missing values (geom_point).

sp + geom_point() + stat_smooth(method=lm,se=FALSE)
## Warning: Removed 1 rows containing non-finite values (stat_smooth).

## Warning: Removed 1 rows containing missing values (geom_point).

sp + geom_point(colour="grey60") + stat_smooth(method=lm,se=FALSE,colour="black")
## Warning: Removed 1 rows containing non-finite values (stat_smooth).

## Warning: Removed 1 rows containing missing values (geom_point).

sp + geom_point(colour="grey60") + stat_smooth(method=loess)
## Warning: Removed 1 rows containing non-finite values (stat_smooth).

## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata,aes(x=Income,y=Expenditure))+geom_point(position = position_jitter(width = 0.3,height=0.06),alpha=0.4,shape=21,size=1.5)+stat_smooth(method = glm,family=binomial)
## Warning: Ignoring unknown parameters: family
## Warning: Removed 1 rows containing non-finite values (stat_smooth).
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata, aes(x=Income, y=Expenditure, colour=Region)) +geom_point() + geom_line(mydata=mydata)
## Warning: Ignoring unknown parameters: mydata

## Warning: Removed 1 rows containing missing values (geom_point).
## Warning: Removed 1 rows containing missing values (geom_path).

model <- lm(Income ~ Province, mydata)
ggplot(mydata, aes(x=Income, y=Expenditure)) + geom_point() + geom_rug()
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata, aes(x=Income, y=Expenditure)) + geom_point() + geom_rug(position = "jitter",size=.2)
## Warning: Removed 1 rows containing missing values (geom_point).

##chapter#6 ##Histogram

ggplot(mydata,aes(x=Income))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (stat_bin).

w<-mydata$Income
ggplot(NULL,aes(x=w))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (stat_bin).

h<-ggplot(mydata,aes(x=Income))
library(MASS)
ggplot(mydata,aes(x=Income))+geom_histogram(fill="white",colour="black")+facet_grid(Province~.)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (stat_bin).

ggplot(mydata,aes(x=Income))+geom_histogram(fill="white",colour="black")+facet_grid(.~Province)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (stat_bin).

ggplot(mydata,aes(x=Income))+geom_histogram(fill="blue",colour="black")+facet_grid(Region~.)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (stat_bin).

ggplot(mydata,aes(x=Income))+geom_histogram(fill="blue",colour="black")+facet_grid(Region~.,scales = "free")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (stat_bin).

ggplot(mydata,aes(x=Expenditure,fill="Region"))+geom_histogram(position = "identity",alpha=0.4)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

##density curve
ggplot(mydata,aes(x=Expenditure))+geom_density()

ggplot(mydata,aes(x=Income))+geom_density()
## Warning: Removed 1 rows containing non-finite values (stat_density).

ggplot(mydata,aes(x=Expenditure))+geom_line(stat = "density")+expand_limits(y=0)

ggplot(mydata,aes(x=Expenditure))+geom_line(stat = "density",adjust=0.25,colours="red")+geom_line(stat = "density")+geom_line(stat = "density",adjust=2,colour="blue")
## Warning: Ignoring unknown parameters: colours

ggplot(mydata,aes(x=Expenditure))+geom_density(fill="blue",alpha=.2)+xlim(6,14)
## Warning: Removed 276 rows containing non-finite values (stat_density).

ggplot(mydata,aes(x=Expenditure))+geom_density(fill="blue",colour=NA,alpha=.2)+geom_line(stat="density")+xlim(6,14)
## Warning: Removed 276 rows containing non-finite values (stat_density).
## Warning: Removed 276 rows containing non-finite values (stat_density).

ggplot(mydata,aes(x=Expenditure,y=..density..))+geom_histogram(fill="cornsilk",colour="grey60",size=.2)+geom_density()+xlim(8,14)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 276 rows containing non-finite values (stat_bin).

## Warning: Removed 276 rows containing non-finite values (stat_density).

mydata1<-mydata
mydata1$Province<-factor(mydata1$Province)
ggplot(mydata1,aes(x=Expenditure,colour=Province))+geom_density()

ggplot(mydata1,aes(x=Expenditure,fill=Province))+geom_density(alpha=.5)

ggplot(mydata1,aes(x=Expenditure,))+geom_density()+facet_grid(Province~.)

##frequency polygon
ggplot(mydata,aes(x=Expenditure))+geom_freqpoly()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

##Boxplot
ggplot(mydata,aes(x=factor(Region),y=Income))+geom_boxplot()
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata,aes(x=factor(Region),y=Income))+geom_boxplot(width=0.4)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata,aes(x=factor(Region),y=Income))+geom_boxplot(outlier.size = 1.5,outlier.shape = 21)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata,aes(x=1,y=Income))+geom_boxplot()+scale_x_continuous(breaks = NULL)+theme(axis.title.x = element_blank())
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata,aes(x=factor(Region),y=Income))+geom_boxplot(notch = TRUE)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata,aes(x=factor(Region),y=Income))+geom_boxplot()+stat_summary(fun.y = "mean",geom = "point",shape=23,size=3,fill="white")
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).
## Warning: Removed 1 rows containing non-finite values (stat_summary).

##Violin Plot
p<-ggplot(mydata,aes(x=Region,y=Income))
p+geom_violin()
## Warning: Removed 1 rows containing non-finite values (stat_ydensity).

p+geom_violin(trim = FALSE)
## Warning: Removed 1 rows containing non-finite values (stat_ydensity).

p+geom_violin(scale = "count")
## Warning: Removed 1 rows containing non-finite values (stat_ydensity).

p+geom_violin(adjust=.22)
## Warning: Removed 1 rows containing non-finite values (stat_ydensity).

##Dot Plot
p<-ggplot(mydata,aes(x=Income,y=Expenditure))
p+geom_point()+stat_density2d()
## Warning: Removed 1 rows containing non-finite values (stat_density2d).
## Warning: Removed 1 rows containing missing values (geom_point).

p+stat_density2d(aes(colour=..level..))
## Warning: Removed 1 rows containing non-finite values (stat_density2d).

p+stat_density2d(aes(fill=..density..),geom = "raster",contour = FALSE)
## Warning: Removed 1 rows containing non-finite values (stat_density2d).

p+geom_point()+stat_density2d(aes(alpha=..density..),geom = "tile",contour = FALSE)
## Warning: Removed 1 rows containing non-finite values (stat_density2d).

## Warning: Removed 1 rows containing missing values (geom_point).

p+stat_density2d(aes(fill=..density..),geom = "raster",contour = FALSE,h=c(.5,5))
## Warning: Removed 1 rows containing non-finite values (stat_density2d).

##chapter#7

p<-ggplot(mydata,aes(x=Income,y=Expenditure))+geom_point()
p+annotate("text",x=3,y=15,label="Group1")+annotate("text",x=5,y=18,label="Group2")
## Warning: Removed 1 rows containing missing values (geom_point).

p+annotate("text",x=3,y=15,label="Group1",family="serif",fontface="italic",colour="darkred",size=3)+annotate("text",x=5,y=18,label="Group2",family="serif",fontface="italic",colour="darkred",size=3)
## Warning: Removed 1 rows containing missing values (geom_point).

p+annotate("text",x=3,y=15,label="Group1",alpha=.1)+annotate("text",x=5,y=18,label="Group2",alpha=.1)
## Warning: Removed 1 rows containing missing values (geom_point).

p + annotate("text", x=-Inf, y=Inf, label="Upper left", hjust=-.2, vjust=2) +annotate("text", x=mean(range(mydata$Income)), y=-Inf, vjust=-0.4,label="Bottom middle")
## Warning: Removed 1 rows containing missing values (geom_point).
## Warning: Removed 1 rows containing missing values (geom_text).

p + annotate("text", x=2, y=0.3, parse=TRUE,label="frac(1, sqrt(2 * pi)) * e ^ {-x^2 / 2}")
## Warning: Removed 1 rows containing missing values (geom_point).

p + annotate("text", x=0, y=0.05, parse=TRUE, size=4,label="'Function: ' * y==frac(1, sqrt(2*pi)) * e^{-x^2/2}")
## Warning: Removed 1 rows containing missing values (geom_point).

p<-ggplot(mydata,aes(x=Income,y=Expenditure,colour=Region))+geom_point()
p+geom_hline(yintercept = 35)+geom_vline(xintercept = 6)
## Warning: Removed 1 rows containing missing values (geom_point).

p + geom_abline(intercept=37.4, slope=1.75)
## Warning: Removed 1 rows containing missing values (geom_point).

hw_means <- ddply(mydata, "Region", summarise, Expenditure=mean(Expenditure))
p + geom_hline(aes(yintercept=Expenditure, colour=Region), mydata=hw_means,linetype="dashed", size=1)
## Warning: Ignoring unknown parameters: mydata
## Warning: Removed 1 rows containing missing values (geom_point).

pg <- ggplot(mydata, aes(x=Province, y=Income)) + geom_point()
pg + geom_vline(xintercept = 2)
## Warning: Removed 1 rows containing missing values (geom_point).

pg + geom_vline(xintercept = which(levels(mydata$Province)=="ctrl"))
## Warning: Removed 1 rows containing missing values (geom_point).

p <- ggplot(subset(mydata, Region=="Region"), aes(x=Expenditure, y=Income)) +geom_line()
p + annotate("segment", x=8, xend=11, y=6, yend=11)

p + annotate("segment", x=8, xend=11, y=6, yend=11, colour="blue",size=2, arrow=arrow()) +annotate("segment", x=10, xend=12, y=-8, yend=15,arrow=arrow(ends="both", angle=90, length=unit(.2,"cm")))

pg<-mydata
pg$Region<-"no"
pg$Region[pg$Province=="trt2"] <- "yes"
ggplot(pg, aes(x=Province, y=Expenditure, fill=Region)) + geom_boxplot() +scale_fill_manual(values=c("grey85", "#FFDDCC"), guide=FALSE)

ggplot(mydata, aes(x=Province, y=Income, fill=Region)) + geom_boxplot() +scale_fill_manual(values=c("grey85", "grey85", "#FFDDCC"), guide=FALSE)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata, aes(x=Province, y=Income, fill=Region)) +geom_bar(position="dodge",stat = "identity") +geom_errorbar(aes(ymin=Income-Expenditure, ymax=Income+Expenditure),position="dodge", width=.2)
## Warning: Removed 1 rows containing missing values (geom_bar).
## Warning: Removed 1 rows containing missing values (geom_errorbar).

ggplot(mydata, aes(x=Province, y=Income, fill=Region)) +geom_bar(position="dodge",stat = "identity") +geom_errorbar(aes(ymin=Income-Expenditure, ymax=Income+Expenditure),position="dodge", width=.2)
## Warning: Removed 1 rows containing missing values (geom_bar).

## Warning: Removed 1 rows containing missing values (geom_errorbar).

p <- ggplot(mydata, aes(x=Income, y=Expenditure)) + geom_point() + facet_grid(. ~ Province)

###chapter#8 Axes

ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot()
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot() + coord_flip()
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot() + coord_flip()+scale_x_discrete(limits=rev(levels(mydata$Region)))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p<-ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot()
p
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p+ylim(0,max(mydata$Income))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ylim(0, 10)
## <ScaleContinuousPosition>
##  Range:  
##  Limits:    0 --   10
scale_y_continuous(limits=c(0, 10))
## <ScaleContinuousPosition>
##  Range:  
##  Limits:    0 --   10
p + ylim(0, 10) + scale_y_continuous(breaks=NULL)
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + scale_y_continuous(breaks=NULL) + ylim(0, 10)
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.
## Warning: Removed 276 rows containing non-finite values (stat_boxplot).

p + scale_y_continuous(limits=c(0, 10), breaks=NULL)
## Warning: Removed 276 rows containing non-finite values (stat_boxplot).

p + scale_y_continuous(limits = c(5, 12))
## Warning: Removed 276 rows containing non-finite values (stat_boxplot).

p + coord_cartesian(ylim = c(6, 14))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + expand_limits(y=0)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot() + scale_y_reverse()
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot() + ylim(8, 10)
## Warning: Removed 276 rows containing non-finite values (stat_boxplot).

sp <- ggplot(mydata, aes(x=Income,y=Expenditure)) + geom_point()
sp + coord_fixed()
## Warning: Removed 1 rows containing missing values (geom_point).

sp + coord_fixed() +scale_y_continuous(breaks=seq(0, 420, 30)) +scale_x_continuous(breaks=seq(0, 420, 30))
## Warning: Removed 1 rows containing missing values (geom_point).

sp + coord_fixed(ratio=1/2) +scale_y_continuous(breaks=seq(0, 420, 30)) +scale_x_continuous(breaks=seq(0, 420, 15))
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot()
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot() +scale_y_continuous(breaks=c(4, 4.25, 4.5, 5, 6, 8))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p <- ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot()
p + theme(axis.text.y = element_blank())
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + theme(axis.ticks = element_blank(), axis.text.y = element_blank())
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + scale_y_continuous(breaks=NULL)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

hwp <- ggplot(mydata, aes(x=Province, y=Income)) +geom_point()
hwp + scale_y_continuous(breaks=c(50, 56, 60, 66, 72),labels=c("Tiny", "Really\nshort", "Short","Medium", "Tallish"))
## Warning: Removed 1 rows containing missing values (geom_point).

bp <- ggplot(mydata, aes(x=Region, y=Income)) + geom_boxplot() +scale_x_discrete(breaks=c("ctrl", "trt1", "trt2"),labels=c("Control", "Treatment 1", "Treatment 2"))
bp
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

bp + theme(axis.text.x = element_text(angle=90, hjust=1, vjust=.5))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

bp + theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

bp + theme(axis.text.x = element_text(family="Times", face="italic",colour="darkred", size=rel(0.9)))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

hwp <- ggplot(mydata, aes(x=Province, y=Income, colour=Region)) +geom_point()
hwp
## Warning: Removed 1 rows containing missing values (geom_point).

hwp + xlab("Total income") + ylab("Total Expenditure")
## Warning: Removed 1 rows containing missing values (geom_point).

hwp + labs(x = "Total Income", y = "Total Expenditure")
## Warning: Removed 1 rows containing missing values (geom_point).

p <- ggplot(mydata, aes(x=Region, y=Expenditure)) + geom_boxplot()
p

p + theme(axis.title.x=element_blank())

p + xlab("")

hwp <- ggplot(mydata, aes(x=Province, y=Income)) + geom_point()
hwp + theme(axis.title.x=element_text(face="italic", colour="darkred", size=14))
## Warning: Removed 1 rows containing missing values (geom_point).

p <- ggplot(mydata, aes(x=Province, y=Income)) + geom_point()
p + theme(axis.line = element_line(colour="black"))
## Warning: Removed 1 rows containing missing values (geom_point).

p + theme_bw() +theme(panel.border = element_blank(),axis.line = element_line(colour="black"))
## Warning: Removed 1 rows containing missing values (geom_point).

p + theme_bw() +theme(panel.border = element_blank(),axis.line = element_line(colour="black", size=4))
## Warning: Removed 1 rows containing missing values (geom_point).

ggplot(mydata, aes(x=Income, y=Expenditure, label=rownames(mydata)))+geom_text(size=3)
## Warning: Removed 1 rows containing missing values (geom_text).

ggplot(mydata, aes(x=Income,y=Expenditure)) + geom_line()
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(mydata, aes(x=Income,y=Expenditure)) + geom_line() +scale_y_log10(breaks=c(2,10,50,250))
## Warning: Removed 1 rows containing missing values (geom_path).

ggplot(mydata, aes(x=Income, fill=Province)) +geom_histogram(binwidth=15, origin=-7.5, colour="black", size=.25) +guides(fill=guide_legend(reverse=TRUE)) +coord_polar() +scale_x_continuous(limits=c(0,360), breaks=seq(0, 360, by=45),minor_breaks=seq(0, 360, by=15)) +scale_fill_brewer()
## Warning: `origin` is deprecated. Please use `boundary` instead.
## Warning: Removed 276 rows containing non-finite values (stat_bin).

##Chapter#9

p <- ggplot(mydata, aes(x=Province, y=Income)) + geom_point()
p + ggtitle("Total Income and Expenditure in a year")
## Warning: Removed 1 rows containing missing values (geom_point).

p + ggtitle("Total Income and Expenditure in a year")+theme(plot.title = element_text(vjust = -2.5))
## Warning: Removed 1 rows containing missing values (geom_point).

p <- ggplot(mydata, aes(x=Income, y=Expenditure)) + geom_point()
p + theme(axis.title.x=element_text(size=16, lineheight=.9, family="Times",face="bold.italic", colour="red"))
## Warning: Removed 1 rows containing missing values (geom_point).
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database

p + ggtitle("Total Income and Expenditure in a year") +theme(plot.title=element_text(size=rel(1.5), lineheight=.9, family="Times",face="bold.italic", colour="red"))
## Warning: Removed 1 rows containing missing values (geom_point).

## Warning: font family not found in Windows font database

## Warning: font family not found in Windows font database

## Warning: font family not found in Windows font database

p <- ggplot(mydata, aes(x=Income, y=Expenditure)) + geom_point()
p + theme_grey()
## Warning: Removed 1 rows containing missing values (geom_point).

p + theme_bw()
## Warning: Removed 1 rows containing missing values (geom_point).

p + theme_grey(base_size=16, base_family="Times")
## Warning: Removed 1 rows containing missing values (geom_point).

## Warning: font family not found in Windows font database

## Warning: font family not found in Windows font database

## Warning: font family not found in Windows font database

## Warning: font family not found in Windows font database

## Warning: font family not found in Windows font database

## Warning: font family not found in Windows font database

## Warning: font family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x,
## x$y, : font family not found in Windows font database

p <- ggplot(mydata, aes(x=Income, y=Expenditure, colour=Region)) + geom_point()
p + theme(panel.grid.major = element_line(colour="red"),panel.grid.minor = element_line(colour="red", linetype="dashed", size=0.2),panel.background = element_rect(fill="lightblue"),panel.border = element_rect(colour="blue", fill=NA, size=2))
## Warning: Removed 1 rows containing missing values (geom_point).

p + ggtitle("Plot title here") +theme(axis.title.x = element_text(colour="red", size=14),axis.text.x = element_text(colour="blue"),axis.title.y = element_text(colour="red", size=14, angle = 90),axis.text.y = element_text(colour="blue"),plot.title = element_text(colour="red", size=20, face="bold"))
## Warning: Removed 1 rows containing missing values (geom_point).

p + theme(legend.background = element_rect(fill="grey85", colour="red", size=1),legend.title = element_text(colour="blue", face="bold", size=14),legend.text = element_text(colour="red"),legend.key = element_rect(colour="blue", size=0.25))
## Warning: Removed 1 rows containing missing values (geom_point).

p + facet_grid(Region ~ .) + theme(strip.background = element_rect(fill="pink"),strip.text.y = element_text(size=14, angle=-90, face="bold"))
## Warning: Removed 1 rows containing missing values (geom_point).

p + theme(axis.title.x = element_text(colour="red")) + theme_bw()
## Warning: Removed 1 rows containing missing values (geom_point).

p + theme_bw() + theme(axis.title.x = element_text(colour="red", size=12))
## Warning: Removed 1 rows containing missing values (geom_point).

p <- ggplot(mydata, aes(x=Income, y=Expenditure)) + geom_point()
p + theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())
## Warning: Removed 1 rows containing missing values (geom_point).

##chapter#10
p <- ggplot(mydata, aes(x=Province, y=Income, fill=Region)) + geom_boxplot()
p
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + guides(fill=FALSE)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + scale_fill_discrete(guide=FALSE)
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + theme(legend.position="none")
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p <- ggplot(mydata, aes(x=Province, y=Income, fill=Region)) + geom_boxplot() +scale_fill_brewer(palette="Pastel2")
p + theme(legend.position="top")
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + theme(legend.position=c(1,0), legend.justification=c(1,0))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + theme(legend.position=c(1,1), legend.justification=c(1,1))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p <- ggplot(mydata, aes(x=Province, y=Income, fill=Region)) + geom_boxplot()
p
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + scale_fill_discrete(limits=c("trt1", "trt2", "ctrl"))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + scale_fill_grey(start=.5, end=1, limits=c("trt1", "trt2", "ctrl"))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + scale_fill_brewer(palette="Pastel2", limits=c("trt1", "trt2", "ctrl"))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + guides(fill=guide_legend(reverse=TRUE))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + labs(fill="Area")
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

hw <- ggplot(mydata, aes(x=Income, y=Expenditure, colour=Region)) +geom_point(aes(size=Expenditure)) + scale_size_continuous(range=c(1,4))
hw
## Warning: Removed 1 rows containing missing values (geom_point).

hw + labs(colour="Rural/Urbar", size="Expenditure\n(Rupees)")
## Warning: Removed 1 rows containing missing values (geom_point).

hw1 <- ggplot(mydata, aes(x=Income, y=Expenditure, shape=Region, colour=Region)) +geom_point()
hw1
## Warning: Removed 1 rows containing missing values (geom_point).

hw1 + labs(shape="Rural/Urban", colour="rural/Urban")
## Warning: Removed 1 rows containing missing values (geom_point).

p + scale_fill_discrete(labels=c("punjab", "sindh ", "kp","balochistan"))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).

p + scale_fill_discrete(labels=c("Urban", "Rural","Type 2\ntreatment")) +theme(legend.text=element_text(lineheight=.8),legend.key.height=unit(1, "cm"))
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).