I'm quite new to programming as well as data analysis, please bear with me here. My data currently consists of a list of 14 matrices (lom), each corresponding to data from a country (with two-letter country codes).
Here is a full sample for Austria:
> lom["AT"]
$`AT`
Year AllKey AllSub SelKey SelSub
1 2000 1.622279 0.5334964 1.892894 0.8057591
2 2001 1.903745 0.5827514 2.291335 0.8295899
3 2002 1.646538 0.4873866 2.006873 0.7360566
4 2003 1.405250 0.8692641 2.105648 1.2711968
5 2004 1.511154 1.5091751 1.970236 1.9407666
6 2005 1.459177 0.6781008 1.808982 1.1362805
7 2006 1.604652 0.5038658 1.942126 0.7992008
8 2007 2.107326 0.9260200 2.683072 1.3302627
9 2008 1.969735 0.6178362 2.994758 1.2051339
10 2009 1.955768 0.7365529 2.896198 1.2272024
11 2010 2.476157 0.7952590 3.715950 1.5686643
12 2011 2.092459 0.4970011 2.766169 0.6476707
13 2012 1.913122 0.5338756 2.450942 0.6022315
14 2013 2.086200 0.6739412 2.786736 0.9211941
15 2014 2.579428 0.8424793 3.152541 1.0225888
16 2015 10.662568 5.8472436 9.769320 3.8840780
17 2016 11.088286 4.6504581 10.567789 3.2383420
18 2017 7.225053 1.7528594 6.747515 1.2781224
I'd like to get all 14 countries plotted against x = Year and y = each of the other variables, i.e. four plots with 14 lines each. Hence the requirement in the question title.
I keep coming up with impossibilities involving some combination of a for
loop and some apply
function, for example:
for (i in colnames(lom$anyCountry)) {
ggplot(lapply(lom, function(x) x[,1:14], aes(x=Year, y=i)
}
which apart from many other problems I can now see throws:
Error:
data
must be a data frame, or other object coercible byfortify()
, not a list
which led me to combine the list of matrices into a big matrix inspired by this:
bigDF <- do.call(rbind, lom)
I suppose I could restructure my data some other way, perhaps I'm missing some functionality that would help... probably both. I would appreciate any pointers as to how to achieve this as efficiently as possible.