r-线图图未连接(财务数据)

时间:2018-09-07 15:55:00

标签: r ggplot2

我正在尝试使用ggplot创建折线图,以显示收到的年度资金(y变量= total_US_received; x变量= year)。

通过图表的外观,我怀疑大量的0:s会影响结果。这些可以被忽略吗?

我正在使用的代码是:

ggplot(countrylevel, aes(x=year, y=Total_US_received)) + geom_line()

The result I get looks like this

enter image description here

任何有关如何改进图形的建议将不胜感激。

编辑:用read_csv2打开数据框,我得到以下结果:

result using read_csv2 enter image description here

这是我的数据(压缩后):

structure(list(year = c(2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L), Total_US_required = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 167L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 104L, 2L, 
2L, 2L, 2L, 63L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 47L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 151L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 111L, 2L, 2L, 2L, 2L, 34L, 
2L, 2L, 2L, 2L, 37L, 2L, 2L, 2L, 141L, 129L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 96L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
117L, 2L, 36L, 2L, 143L, 53L, 2L, 2L, 2L, 2L, 2L, 25L, 2L, 2L, 
2L, 7L, 35L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 185L, 2L, 
2L, 2L, 2L, 99L, 2L, 2L, 206L, 2L, 2L, 2L, 12L, 2L, 2L, 2L, 2L, 
2L, 2L, 124L), .Label = c("", "0", "1,32E+11", "1000000", "100000000", 
"1005205443", "10072701,5", "10200000", "103784200,5", "1040147", 
"104805000", "1071103", "10750000", "1090480364", "109153", "11060703", 
"11068500,5", "11101695", "11354680", "1143893994", "11702455", 
"11956538", "12120011", "12132493", "12410092", "12500000", "12552013", 
"12808836", "129220163", "12940269", "1299891296", "1312327112", 
"13316000", "137911265", "13796751", "13985000", "14300000", 
"14457401", "14781209", "14840854", "1502221330", "1519195", 
"15274599", "1537800", "15617154", "15710660", "16200000", "166500000", 
"16701505", "168584522", "17054786", "1776123", "179338641", 
"18173270,5", "18215196", "18333333,33", "18664384", "187181768", 
"18922351", "19151715", "1918929", "19456115", "199823581", "20000000", 
"200000000", "20020037", "201262789", "2026765", "20379635,5", 
"204000000", "20564664", "20750000", "20800000", "2090176", "212054562", 
"218740000", "220000000", "22024649", "22059615", "221897971", 
"22212807", "2280767", "22815000", "23000000", "230197879", "23161089", 
"2319522", "24980000", "251298009", "26000000", "26450000", "26547636", 
"26566535", "2684877", "2768871", "2805517", "28472211", "28748910,75", 
"28986609", "30000000", "30392419", "30486643", "306186910", 
"3070192", "31170915", "32000000", "32818841", "33500000", "34328000", 
"349570726", "35000000", "35274117", "35475942", "35637337", 
"35732550", "36576823", "3691769,5", "37011827", "38112323", 
"382458072", "38372246", "384900", "38600000", "38677251", "395551054", 
"4013114", "40571983", "4165567", "4250000", "42592000", "4271640", 
"43000000", "4485517", "4500000", "45553092", "45570852", "4600000", 
"46358480", "477078000", "48500000", "48693043", "4969559", "49771737", 
"5000000", "5047653", "52025932", "52534097", "54000000", "54300000", 
"544088494", "546938906", "54712144,5", "553162739", "561892820", 
"57000000", "57400000", "581079038", "582730988", "598981", "59962157", 
"6000000", "603544533", "60589173,6", "606254626", "60750000", 
"6120664", "61506194,5", "6250000", "632366288", "63762742,5", 
"6431433", "651971301", "6594817,5", "6670000", "6671000", "67500000", 
"67723257", "68000000", "68707342,5", "69900000", "7000000", 
"70409614", "70534845", "7071951", "71502922", "718751,5", "72263676", 
"72767987", "739204", "741000000", "76816821", "7712871", "774943253", 
"776000000", "7804442", "782018", "78900000", "79310556", "798807640", 
"8010000", "83045026", "839413371", "86000000", "87699664,5", 
"9052940", "9114864", "980000", "98147205", "993069", "9945998", 
"99676205"), class = "factor"), Total_US_received = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 156L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 39L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 54L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 2L, 2L, 2L, 
2L, 202L, 2L, 2L, 2L, 26L, 144L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 96L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 200L, 2L, 198L, 
2L, 87L, 137L, 2L, 2L, 2L, 2L, 2L, 213L, 2L, 2L, 2L, 165L, 139L, 
2L, 2L, 2L, 51L, 2L, 2L, 2L, 2L, 2L, 2L, 8L, 2L, 2L, 2L, 2L, 
89L, 2L, 2L, 124L, 2L, 2L, 2L, 12L, 2L, 2L, 2L, 65L, 2L, 2L, 
105L), .Label = c("", "0", "1000000", "1015614409", "102196912", 
"10286090", "1040146", "10494288,5", "10559092", "10601640", 
"10660527", "1071103", "10864110", "10900000", "109153", "1107598204", 
"11139934,33", "111645732", "115656", "11702455", "11782593", 
"1200061", "1201967", "12115565", "12136102,5", "12515731", "12554644,5", 
"12619592", "130290299", "13100400", "1321738", "133029804", 
"133937699", "1353062", "13869184", "13957964", "14122864", "14196512", 
"14390000", "14474887", "14603307", "146811271", "14803699", 
"15000000", "1506314", "15099749", "15127408", "15154485", "1519195", 
"1519283", "1523891,2", "153608778", "153702318", "153878654", 
"15422351", "154810248", "1553005", "15600000", "15806841", "16043247", 
"1613606", "16386883", "16688554", "16933060", "1693550", "172646900", 
"172979780", "173803520", "17442963,4", "1750242", "1812580", 
"18327716", "1864608", "18664384", "1884269", "19043876", "1913330", 
"19208777", "1942999", "19500797", "19634851", "1972054", "1973161,05", 
"1999890", "211080741", "21364744", "21478156", "21663285", "21842791", 
"2190455", "22297547", "22573195", "227854767", "2278869", "2280790", 
"2292085", "233449911", "23557901", "23609845", "2367794", "25966989", 
"26225926", "26403842", "26507660", "26582033", "2677014", "2790598", 
"28033068", "280678042", "2950462", "2955888125", "2968275", 
"29708185", "30259405", "30663885", "311370,5", "3117063", "3123107", 
"3136815", "31515034", "3219461", "325000000", "3308400", "3431553", 
"34695229,5", "3500011", "35076700", "357250374", "3589825", 
"3624829", "3652163", "367262924", "3684877", "37115540", "3739681", 
"384833401", "38622368", "386544", "3868422", "4013114", "4094585", 
"4165567", "420653055", "4250000", "42630065", "4268944", "4298100", 
"43500000", "43625712", "4399059", "4457651", "45118120", "454765583", 
"4610465", "46481136", "46521673,5", "467458781", "467833166", 
"481844000", "4839160", "488626274", "5000000", "5185252", "5400292,23", 
"5410657", "54325792", "54382350", "550434569", "559724655", 
"56234467", "563955", "5662911", "5921234", "6120664", "61455087", 
"6182013", "6233540", "6250000", "628473", "636467283", "6376123", 
"6418934", "6431433", "6556366", "665294893", "6732558", "68472934,5", 
"6861041", "6932070", "6959603", "6977580", "69861890", "707902098", 
"709341604", "7107432", "75093648", "7511770", "7615691", "7615961", 
"787785", "78900000", "7900000", "79310556", "8103006", "8137886", 
"8141939,5", "82012623", "82324212", "8429768", "846703", "84731888", 
"85542547", "8624021", "8635262", "872772", "9000002", "9132550", 
"93241740", "9451311", "963065635", "9660536", "99993083"), class = "factor"), 
Total_US_received_from.other = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("", "0", "100000", "1006501", "1074242,12", 
"10743297", "10770310", "1120000", "11699639", "118666908", 
"1192627", "11985500", "12107705", "12122971", "12387942", 
"12486715", "12557462", "126051735", "126215100", "134355436", 
"13666999", "137160", "14202727", "142432873", "14835929", 
"150130445", "15392646", "15712570", "16879598", "16927130", 
"17050442", "179590", "18794938", "1918087", "192628719", 
"2005300", "20359990", "20366277", "208506791", "20955927", 
"21057964", "212000", "22502219", "22505978", "23049962", 
"2426857", "2540961", "25938524", "2644395", "27128997", 
"28974835", "2907023", "295588125", "300000", "32333622", 
"3243632", "3291445", "3407393", "365364799", "3662816", 
"36641177", "41335774", "423000", "4250463", "42925711", 
"436475", "43949647", "442174577", "4436964", "4503686", 
"450800882", "455179863", "4600000", "4726938", "478644822", 
"48043592", "4859884", "4927344", "50000", "500000", "517156572", 
"5231571", "524000000", "5280672", "53344375", "543290", 
"5511374", "5550000", "57689913", "579881", "6000000", "606155575", 
"613505", "615717", "616075", "6270000", "628088534", "6564966", 
"66602217", "6916265", "6948000", "69893696", "700208377", 
"7161617", "728849", "7365738", "75291231", "75570838", "766794", 
"76704121", "7786228", "7813200", "782593", "7866648", "78878806", 
"79893942", "803922830", "8090000", "81536576", "82132042", 
"8487981", "8910678", "89609843", "9362395", "936906", "968669", 
"9858822", "989033335"), class = "factor"), Total_US_received.Emergency.response.plan_Common.humintarian.fund_country.based. = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("", "0", "10000000", "103252715", 
"121000", "13397050", "1400000", "1455021", "1501023", "1622837", 
"180167,43", "1822402", "1963871", "250000", "25877890", 
"2592709", "2787752", "300000", "3125951", "373134", "3751097", 
"37812993", "38249502", "39642858", "43250552", "5000", "500000", 
"50215100", "567540", "59264269", "6282314", "6500000", "706400", 
"8080810", "82000000", "851664", "9963628"), class = "factor"), 
Total_US_received_from.CERF = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 75L, 2L, 2L, 2L, 
2L, 2L, 2L, 16L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 55L, 2L, 2L, 
2L, 2L, 230L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 89L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 226L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 90L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
210L, 2L, 2L, 2L, 2L, 106L, 2L, 2L, 2L, 2L, 67L, 2L, 2L, 
2L, 105L, 156L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 83L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 213L, 2L, 117L, 2L, 113L, 
25L, 2L, 2L, 2L, 2L, 2L, 93L, 2L, 2L, 2L, 181L, 142L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 91L, 222L, 2L, 2L, 2L, 2L, 
204L, 2L, 2L, 128L, 2L, 2L, 2L, 11L, 2L, 2L, 2L, 45L, 2L, 
2L, 24L), .Label = c("", "0", "1000000", "1025464", "10329268", 
"10371212", "10383240", "1040146", "10405408", "106597425", 
"1070103", "1093884", "11006301", "11019952", "1107614", 
"1110469,5", "11269445", "1142148", "11702455", "11721943", 
"11999076", "1200061", "1201967", "12232994", "12365659", 
"12455835", "1265586", "12985139,5", "1299825", "13001015", 
"13347798", "13794282,2", "1400282", "1450242", "1501344", 
"1503314", "1512075", "1516283", "15410406", "1553005", "15645398", 
"1585201", "16690193", "16915466", "1693550", "17003929", 
"17014450", "1747109", "1751722", "1798653", "18017685", 
"1812580", "18512690", "1870201", "1905355", "1940932", "1942999", 
"1949761", "1966070", "1967415", "1971425", "1972054", "1978455", 
"1990385", "1999890", "1999893", "2000000", "2000095", "2000830", 
"20029976", "2006304", "2024378", "2030597", "2032306", "2034923,5", 
"2052680", "2069776", "2074049", "20889885", "2164278", "2179607", 
"2221613", "2253044", "22683472", "2271877", "22839556", 
"2294798", "2354123", "23686839", "24442232,5", "2472028,5", 
"2485827", "2496956", "2503311", "2522639", "25230980", "2532163", 
"25475033", "2577014", "2579188", "2584669", "25854204", 
"25916415,5", "2624107", "2673988", "26746096", "2817063", 
"2840964,5", "28437349", "2892761", "2996920", "3000002", 
"3000909", "3010825", "3038387", "30853702", "3105354", "311370,5", 
"3136815", "3141908", "31528040", "3166825", "3175302", "3245280,5", 
"3291599", "3363540", "3376068", "3431553", "3454485", "3498910", 
"3500011", "3556532", "3570457", "36564849", "3726365", "3738681", 
"3788227", "3795458", "3851878", "3855356", "386544", "3868422", 
"3879893", "3897864", "3994126", "3998941", "4005971", "4010361", 
"4013114", "4019325", "4094585", "4096923,5", "416325", "4165567", 
"4218944", "4250000", "4278128713", "4320080", "4390369", 
"4457674", "4529592", "4679803", "4736155", "4782918", "4787929", 
"4798922", "4829690", "4834117", "4839160", "4983445", "4989386", 
"4997385", "5000000", "5051640", "5054519", "5102132", "5167962", 
"5200000", "5352736", "5400292", "5410657", "5522753", "5537051", 
"5580667", "563955", "5842338", "5972098", "5986081", "5988888", 
"6000000", "60534026", "6141383", "6220011", "6239930", "6248385,5", 
"628473", "6300221", "6376123", "6431433", "6450266", "6607366", 
"6720259,5", "6932070", "7169618", "7170921", "718751,5", 
"7367516", "751772", "7647237", "7716263", "773181", "777854", 
"787785", "8015052", "8022382", "8110712", "8137886", "846703", 
"8525924", "86172894", "8784528", "8949288,5", "9133227", 
"9138176", "914395", "938652", "9568038", "9920625", "9958731", 
"9972984"), class = "factor")), row.names = c(NA, 200L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

从您的数据df开始,我们可以将因子字段转换为字符,然后将逗号替换为小数,然后将其转换为数字。

library(dplyr)
df2 <- df %>%
  mutate_if(is.factor, as.character) %>%
  mutate_if(is.character, ~sub(",",".",.) %>% as.numeric)

您可能会对每年的总计感兴趣。这是一种按年份总计每一列的方法:

df_annual_total <- df2
  group_by(year) %>%
  summarize_all(sum)