简体   繁体   English

在R矩阵列表中的每个矩阵中绘制一列

[英]Plot one column in every matrix in a list of matrices in R

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). 我的数据当前包含14个矩阵(lom)的列表,每个矩阵对应于一个国家/地区的数据(带有两个字母的国家/地区代码)。

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, ie four plots with 14 lines each. 我想针对x = Year和y =每个其他变量绘制所有14个国家/地区,即四个具有14条线的地块。 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循环和apply函数的某种组合,例如:

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 by fortify() , not a list 错误: data必须是数据框或其他可由fortify()强制执行的对象,而不是列表

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. 对于如何尽可能有效地实现这一目标,我将不胜感激。

Consider appending all matrix data into a master, single data frame with a country indicator that you can use for the color argument of line plots: 考虑将所有矩阵数据附加到具有国家/地区指示器的主单一数据框中,该指示器可用于线图的color参数:

# CREATE LARGE DATAFRAME FROM MATRIX LIST
lom_df <- do.call(rbind, lapply(lom, data.frame))

# CREATE COLUMN NAMES FROM ROWNAMES
lom_df$country <- gsub("\\..*$", "", row.names(lom_df))
row.names(lom_df) <- NULL

# EXTRACT ALL FOUR Y COLUMN NAMES (MINUS Year AND country)
y_columns <- colnames(lom_df[2:(ncol(lom_df)-1)])

# PRODUCE LIST OF FOUR PLOTS EACH WITH COUNTRY LINES
plot_list <- lapply(y_columns, function(col)
  ggplot(lom_df, aes_string(x="Year", y=col, color="country")) +
     geom_line()
)

# OUTPUT EACH LIST 
plot_list

This solution uses package ggplot2 . 该解决方案使用软件包ggplot2

It has two steps, data preparation and plotting. 它有两个步骤,数据准备和绘图。

First of all the list must be transformed into one large data frame, with a column as an id column. 首先,必须将列表转换为一个大数据帧,并将其列作为id列。 I have searched SO for a function that does this but couldn't find one so here it goes. 我已经在SO中搜索了一个可以执行此操作的函数,但是找不到它,所以就可以了。

rbindWithID <- function(x, id.name = "ID", sep = "."){
    if(is.null(names(x))) names(x) <- paste(id.name, seq_along(x), sep = sep)
    res <- lapply(names(x), function(nm){
        DF <- x[[nm]]
        DF[[id.name]] <- nm
        x[[nm]] <- cbind(DF[ncol(DF)], DF[-ncol(DF)])
        x[[nm]]
    })
    do.call(rbind, res)
}

lom_df <- rbindWithID(lom, "Country")

Now reshape the data frame from wide to long. 现在,将数据帧的形状从宽变长。

molten <- reshape2::melt(lom_df, id.vars = c("Country", "Year"))

Finally, plot it. 最后,绘制它。

library(ggplot2)

ggplot(molten, aes(Year, value, colour = Country)) +
    geom_line() +
    facet_wrap(~ variable)

在此处输入图片说明

DATA. 数据。

set.seed(1234)    # Make the results reproducible

lom <- lapply(1:4, function(i){
    data.frame(
        Year = 2000:2008,
        AllKey = runif(9, 1, 2),
        AllSub = runif(9, 0, 2),
        SelKey = runif(9, 1, 2),
        SelSub = runif(9, 0, 2)
    )
})

names(lom) <- c("AT", "DE", "FR", "PT")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM