geom_area fill not working ggplot2

odevsej picture odevsej · Oct 28, 2015 · Viewed 7.2k times · Source

I'm trying to do a stacked area plot in ggplot2. I seem to have followed the code correctly but the output is different. The code seems to work but the areas aren't exactly filled. Anything wrong with my code? or is it the ggplot2 package? Code below:

ggplot(stacks, aes(x=year, y=remit, fill=polcat3,  colour=polcat3) ) + geom_area(aes(fill=polcat3), position='fill')

my plot

My data

year,polcat3,remit
2000,Auto,57131999.97
2000,Auto,139199996.9
2000,Auto,4821840600
2000,Auto,10352872.85
2000,Auto,2852000000
2000,Auto,2161018799
2000,Auto,103622840.9
2000,Auto,39000000
2000,Auto,1075000000
2000,Auto,56885414.12
2000,Auto,180000000
2000,Auto,1340000000
2000,Demo,86343658.45
2000,Demo,87064697.27
2000,Demo,1967528809
2000,Demo,58235450.74
2000,Demo,126870002.7
2000,Demo,1649354980
2000,Demo,26245557.79
2000,Demo,1610122925
2000,Demo,135975250.2
2000,Demo,1838800049
2000,Demo,1322301025
2000,Demo,596200012.2
2000,Demo,641289794.9
2000,Demo,280652252.2
2000,Demo,1190199951
2000,Demo,12883465820
2000,Demo,891700012.2
2000,Demo,49942501.07
2000,Demo,72300003.05
2000,Demo,178600006.1
2000,Demo,7524542969
2000,Demo,73163955.69
2000,Demo,12000000
2000,Demo,177000000
2000,Demo,746886.1938
2000,Demo,9450584.412
2000,Demo,111498191.8
2000,Demo,16399999.62
2000,Demo,6961000000
2000,Demo,278000000
2000,Demo,96000000
2000,Demo,1275199951
2000,Demo,233485305.8
2000,Demo,1765400024
2000,Demo,1696785400
2000,Demo,38100002.29
2000,Demo,4560000000
2000,Demo,33000000
2000,Demo,17000000
2000,Demo,343705993.7
2000,Demo,13300000.19
2000,Semi,597799987.8
2000,Semi,67335105.9
2000,Semi,119317176.8
2000,Semi,790000000
2000,Semi,53158882.14
2000,Semi,273500000
2000,Semi,32400001.53
2000,Semi,536000000
2000,Semi,1845134033
2000,Semi,121802436.8
2000,Semi,537900024.4
2000,Semi,8844514.847
2000,Semi,1165826782
2000,Semi,478087554.9
2000,Semi,342368408.2
2000,Semi,14424637.79
2000,Semi,1391800049
2000,Semi,717663818.4
2000,Semi,7226507.187
2000,Semi,34234287.26
2000,Semi,795953369.1
2000,Semi,8000015.259
2000,Semi,238100006.1
2000,Semi,1288000000
2001,Auto,104125000
2001,Auto,149199996.9
2001,Auto,6538500000
2001,Auto,12119418.14
2001,Auto,2911399902
2001,Auto,3260920166
2001,Auto,117284111
2001,Auto,39011703.49
2001,Auto,1461000000
2001,Auto,52876461.03
2001,Auto,170000000
2001,Auto,1100000000
2001,Demo,189600006.1
2001,Demo,83602699.28
2001,Demo,2104551514
2001,Demo,826200317.4
2001,Demo,135279998.8
2001,Demo,1774780029
2001,Demo,25919340.13
2001,Demo,2056358887
2001,Demo,198443420.4
2001,Demo,1981800049
2001,Demo,1420558105
2001,Demo,45900001.53
2001,Demo,633753601.1
2001,Demo,746951049.8
2001,Demo,296019500.7
2001,Demo,1046000000
2001,Demo,14273018550
2001,Demo,1058300049
2001,Demo,1184989990
2001,Demo,401579681.4
2001,Demo,79150001.53
2001,Demo,112400001.5
2001,Demo,243320007.3
2001,Demo,10146262700
2001,Demo,88167266.85
2001,Demo,25000000
2001,Demo,215000000
2001,Demo,8815530.777
2001,Demo,146985153.2
2001,Demo,73099998.47
2001,Demo,753211425.8
2001,Demo,8769000000
2001,Demo,263799987.8
2001,Demo,116000000
2001,Demo,1402890015
2001,Demo,304678405.8
2001,Demo,1926000000
2001,Demo,1252447510
2001,Demo,40900001.53
2001,Demo,2786000000
2001,Demo,141000000
2001,Demo,6038.0301
2001,Demo,19000000
2001,Demo,297390441.9
2001,Demo,11899999.62
2001,Semi,699299987.8
2001,Semi,49915523.53
2001,Semi,116004821.8
2001,Semi,670000000
2001,Semi,18313718.8
2001,Semi,181300003.1
2001,Semi,682000000
2001,Semi,2011334106
2001,Semi,171272735.6
2001,Semi,550000000
2001,Semi,11113728.52
2001,Semi,746885.9553
2001,Semi,367105255.1
2001,Semi,21972925.19
2001,Semi,1166599976
2001,Semi,5924500.942
2001,Semi,68502540.59
2001,Semi,927148376.5
2001,Semi,15253000.26
2001,Semi,348574798.6
2001,Semi,1294612549
2002,Auto,181707000.7
2002,Auto,140300003.1
2002,Auto,10292581917.288
2002,Auto,2893100098
2002,Auto,204929046.6
2002,Auto,2877152588
2002,Auto,106488113.4
2002,Auto,678485900.9
2002,Auto,39011703.49
2002,Auto,3554000000
2002,Auto,44669322.97
2002,Auto,135000000
2002,Auto,1770000000
2002,Demo,733570007.3
2002,Demo,206630004.9
2002,Demo,76230842.59
2002,Demo,2858057861
2002,Demo,1176951538
2002,Demo,112900001.5
2002,Demo,2448964111
2002,Demo,27150941.85
2002,Demo,2479942627
2002,Demo,250646484.4
2002,Demo,2194300049
2002,Demo,1438022827
2002,Demo,43520000.46
2002,Demo,1600192017
2002,Demo,438744873
2002,Demo,278989471.4
2002,Demo,1258917603
2002,Demo,15735736330
2002,Demo,1260300049
2002,Demo,433000000
2002,Demo,1309079956
2002,Demo,389541656.5
2002,Demo,109248046.9
2002,Demo,138399993.9
2002,Demo,323739990.2
2002,Demo,11029448240
2002,Demo,137653686.5
2002,Demo,56312149.05
2002,Demo,215000000
2002,Demo,7959585.667
2002,Demo,84599998.47
2002,Demo,705382507.3
2002,Demo,9735000000
2002,Demo,201500000
2002,Demo,143000000
2002,Demo,1359354980
2002,Demo,344496490.5
2002,Demo,1953699951
2002,Demo,1379995483
2002,Demo,79099998.47
2002,Demo,1936000000
2002,Demo,209000000
2002,Demo,36053001.4
2002,Demo,19000000
2002,Demo,288378570.6
2002,Demo,12399999.62
2002,Semi,58734134.67
2002,Semi,120092422.5
2002,Semi,1176490.545
2002,Semi,1070000000
2002,Semi,32986892.7
2002,Semi,230453414.9
2002,Semi,851000000
2002,Semi,2143018799
2002,Semi,36716598.51
2002,Semi,843693.614
2002,Semi,435000000
2002,Semi,18965887.07
2002,Semi,1208900024
2002,Semi,5622274.399
2002,Semi,103212654.1
2002,Semi,1070313232
2002,Semi,12000000
2002,Semi,422582519.5
2002,Semi,1294000000
2003,Auto,170957000.7
2003,Auto,222399993.9
2003,Auto,14542468993.9088
2003,Auto,2960899902
2003,Auto,147501953.1
2003,Auto,3613916504
2003,Auto,85281295.78
2003,Auto,771072143.6
2003,Auto,39011703.49
2003,Auto,3964000000
2003,Auto,65225296.02
2003,Auto,889000000
2003,Auto,2100000000
2003,Demo,888748596.2
2003,Demo,273420013.4
2003,Demo,55357849.12
2003,Demo,3191664795
2003,Demo,1718485474
2003,Demo,158166412.4
2003,Demo,2821291016
2003,Demo,38872653.96
2003,Demo,3076018555
2003,Demo,320879547.1
2003,Demo,2325399902
2003,Demo,1633427002
2003,Demo,65100158.69
2003,Demo,2146959961
2003,Demo,516599182.1
2003,Demo,294864593.5
2003,Demo,1488709351
2003,Demo,20999150390
2003,Demo,1398400024
2003,Demo,538000000
2003,Demo,556885986.3
2003,Demo,114833999.6
2003,Demo,173100006.1
2003,Demo,486630004.9
2003,Demo,16653685550
2003,Demo,153798950.2
2003,Demo,128600006.1
2003,Demo,215000000
2003,Demo,12216717.72
2003,Demo,106800003.1
2003,Demo,868500000
2003,Demo,10243000000
2003,Demo,222100006.1
2003,Demo,124000000
2003,Demo,1452994995
2003,Demo,510932281.5
2003,Demo,2122399902
2003,Demo,1607350342
2003,Demo,86800003.05
2003,Demo,729000000
2003,Demo,330000000
2003,Demo,61753292.08
2003,Demo,208000000
2003,Demo,434367492.7
2003,Demo,11800000.19
2003,Semi,53747390.75
2003,Semi,141603500.4
2003,Semi,12388155.94
2003,Semi,1750000000
2003,Semi,46470191.96
2003,Semi,235979049.7
2003,Semi,1178000000
2003,Semi,2201128418
2003,Semi,78157081.6
2003,Semi,1437750000
2003,Semi,571052612.3
2003,Semi,25481748.58
2003,Semi,1062800049
2003,Semi,6931479.454
2003,Semi,148687118.5
2003,Semi,1250253906
2003,Semi,8899999.619
2003,Semi,298785186.8
2003,Semi,1269900024
2003,Semi,36299999.24
2004,Auto,227578002.9
2004,Auto,256700012.2
2004,Auto,19578498944.7746
2004,Auto,3340699951
2004,Auto,1032000000
2004,Auto,165835662.8
2004,Auto,4220835938
2004,Auto,117792945.9
2004,Auto,822608947.8
2004,Auto,39011703.49
2004,Auto,3945000000
2004,Auto,82797576.9
2004,Auto,855000000
2004,Auto,2310000000
2004,Demo,1160672119
2004,Demo,311779998.8
2004,Demo,63013359.07
2004,Demo,3583817139
2004,Demo,1722769531
2004,Demo,210558670
2004,Demo,3575069092
2004,Demo,91521476.75
2004,Demo,3189709961
2004,Demo,319546356.2
2004,Demo,244929977.4
2004,Demo,2501199951
2004,Demo,1838041748
2004,Demo,303235168.5
2004,Demo,82371200.56
2004,Demo,2627500000
2004,Demo,664671203.6
2004,Demo,1716792236
2004,Demo,1866314941
2004,Demo,18750376950
2004,Demo,1623198120
2004,Demo,620000000
2004,Demo,627204895
2004,Demo,324496734.6
2004,Demo,229399993.9
2004,Demo,705239990.2
2004,Demo,19861748050
2004,Demo,155463485.7
2004,Demo,202500000
2004,Demo,215000000
2004,Demo,15236835.48
2004,Demo,59712108.61
2004,Demo,108900001.5
2004,Demo,1132713623
2004,Demo,11471000000
2004,Demo,238199996.9
2004,Demo,132000000
2004,Demo,2495106934
2004,Demo,632895324.7
2004,Demo,2564100098
2004,Demo,1621875977
2004,Demo,86900001.53
2004,Demo,804000000
2004,Demo,411000000
2004,Demo,69896163.94
2004,Demo,143000000
2004,Demo,522837402.3
2004,Demo,11600000.38
2004,Semi,44417327.88
2004,Semi,159198211.7
2004,Semi,14954054.83
2004,Semi,2460000000
2004,Semi,133742813.1
2004,Semi,2330324463
2004,Semi,188672546.4
2004,Semi,58446876.53
2004,Semi,1589569946
2004,Semi,801868408.2
2004,Semi,2272699951
2004,Semi,9526646.614
2004,Semi,179017074.6
2004,Semi,1431351074
2004,Semi,13798418.05
2004,Semi,310532470.7
2004,Semi,1282598999
2004,Semi,48400001.53
2005,Auto,623235000
2005,Auto,198600000
2005,Auto,8831739256.9678
2005,Auto,1032000000
2005,Auto,62021921.3283798
2005,Auto,4589226819.79732
2005,Auto,129488927.894728
2005,Auto,1211823252.50637
2005,Auto,39011703.5110533
2005,Auto,4280000000
2005,Auto,95421869.7624513
2005,Auto,823000000
2005,Auto,3150000000
2005,Demo,1289704315.92613
2005,Demo,432090000
2005,Demo,146880908.452325
2005,Demo,4642385283.80997
2005,Demo,1612912427.00584
2005,Demo,337040056.699114
2005,Demo,2805358000
2005,Demo,117891862.0425
2005,Demo,3345610321.40797
2005,Demo,420346410
2005,Demo,104817181.330673
2005,Demo,2719200000
2005,Demo,2460000000
2005,Demo,446005398.63
2005,Demo,99184576
2005,Demo,3066600000
2005,Demo,692807908.925609
2005,Demo,1912955033.65206
2005,Demo,5419620728.75305
2005,Demo,22125089480.4982
2005,Demo,1761628767.01559
2005,Demo,424991045.929029
2005,Demo,4924310615.23333
2005,Demo,599357676.667103
2005,Demo,534305076.983413
2005,Demo,381000000
2005,Demo,915080000
2005,Demo,22741840918
2005,Demo,177186056.198184
2005,Demo,180360000
2005,Demo,609194.496202642
2005,Demo,22531928.6466736
2005,Demo,17561579.0633268
2005,Demo,66369885.0351597
2005,Demo,129600000
2005,Demo,1440067536.07357
2005,Demo,13732557721.8325
2005,Demo,161300000
2005,Demo,4707910000
2005,Demo,3436552689.38484
2005,Demo,788824890.465872
2005,Demo,3028600000
2005,Demo,1187095814.94597
2005,Demo,92400000
2005,Demo,887000000
2005,Demo,2408000000
2005,Demo,76740390
2005,Demo,148000000
2005,Demo,613577432.378763
2005,Demo,13000000
2005,Semi,56642283.3408211
2005,Semi,163173835.554478
2005,Semi,11375094.5558432
2005,Semi,169999999.999993
2005,Semi,5017300000
2005,Semi,173519405.683879
2005,Semi,2420874471.08604
2005,Semi,313250258.3113
2005,Semi,31854955.6480974
2005,Semi,1975536000
2005,Semi,1116972560.32095
2005,Semi,14640084346.8768
2005,Semi,6866648.84058121
2005,Semi,192500458.801737
2005,Semi,1392672712.79192
2005,Semi,19366300
2005,Semi,321808492.95616
2005,Semi,1282599000
2005,Semi,8900000
2005,Semi,52870000

Answer

JHowIX picture JHowIX · Oct 28, 2015

The geom_area charts are just geom_ribbon charts with the range fixed to 0. Since you have multiple overlapping points in your ribbon chart, I think you need to modify your position tag to 'stack' instead of 'fill'.

You also might need to tell ggplot to provide an order to your raw y variable by using:

ggplot(stacks, aes(x=year, y=order(remit), fill=polcat3,  colour=polcat3)

Without seeing your data finding the root cause is difficult