从列表中的多个项目访问相同的向量

时间:2014-07-09 12:24:40

标签: r list plot

我有一个包含大量data.frames的列表(部分显示如下)。从每个数据框架,我想访问两个用于绘图的向量('年与值'的曲线)。向量在每个data.frame中具有相同的名称。

List of 30
$ A            :'data.frame':   36 obs. of  2 variables:
  ..$ Year                   : int [1:36] 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 ...
  ..$ Value                  : int [1:36] 123 222 254 325 658 965 458 12 25 ... 
 $ B            :'data.frame':  39 obs. of  2 variables:
  ..$ Year                   : int [1:39] 1968 1969 1970 1970 1971 1972 1973 1974 1975 1976 ...
  ..$ Value                  : int [1:39] 55 89 65 258 96 546 254 159 365 214 ... 
 $ C            :'data.frame':  26 obs. of  2 variables:
  ..$ Year                   : int [1:26] 1960 1961 1962 1963 1964 1965 1966 1967 1968    1969 ...
  ..$ Value                  : int [1:26] 145 156 250 260 280 311 315 360 390 388 348 666 ... 

我尝试使用matplot进行绘图,但我尝试过的语法不起作用:

matplot(mylist[[0:30]]$Year, mylist[[0:30]]$Value) 

如何创建一个图表,以不同颜色显示列表中每个data.frames的曲线,而曲线在不同年份开始/停止?

3 个答案:

答案 0 :(得分:1)

为什么不使用简单的循环?
例如:

# create a random mylist containing 3 data.frame's (use yours instead)
set.seed(1234) 

mylist <- list()
mylist$A <- data.frame(Year=1971:1980,Value=runif(10))
mylist$B <- data.frame(Year=1972:1978,Value=runif(7))
mylist$C <- data.frame(Year=1974:1981,Value=runif(8))
# ~

# create a color palette to give one color to each curve
# you can use other functions returning palettes like
# heat.colors, topo.colors etc...
colors <- rainbow(length(mylist))
# ~

# create an initial empty plot big enough to contain all the curves
allYears <- unlist(sapply(mylist,FUN=function(x)x$Year))
allValues <- unlist(sapply(mylist,FUN=function(x)x$Value))

plot(x=c(min(allYears),max(allYears)),
     y=c(min(allValues),max(allValues)),
     type='n',
     xlab='Value',
     ylab='Year',
     main='Yearly Values')
# ~

# for each data.frame in mylist add a line curve 
for(i in 1:length(mylist)){
  lines(mylist[[i]]$Year, mylist[[i]]$Value,col=colors[i])
}
# ~

# uncomment the following line to add a legend
# legend("topright",legend=names(mylist), fill=colors)
# ~

enter image description here

答案 1 :(得分:1)

感谢您的评论。针对不同行数调整的代码

# create some data
set.seed(321)
A <- data.frame(Year = seq(1974, length.out=36),
                Value = runif(n=36, min=1, max=700))

B <- data.frame(Year = seq(1968, length.out=39), 
                Value = runif(n=39, min=1, max=700))

C <- data.frame(Year = seq(1960, length.out=26), 
                Value = runif(n=26, min=1, max=700))

mylist <- list(A=A, B=B, C=C)

mydf <- Reduce(function(...) merge(..., by="Year", all=T), mylist)
colnames(mydf) <- c("Year", names(mylist))
values.mx <- as.matrix(mydf[, -1])
years.mx <- as.matrix(mydf[, 1])

matplot(x=years.mx, y=values.mx, xlab="Year", ylab="Value")

对于matplot,您可以添加type =&#39; l&#39;对于曲线

答案 2 :(得分:0)

或者如果您对非base套餐感到满意:

mylist <- list(A = data.frame(Year = c(2011, 2012), Value = 1:2),
               B = data.frame(Year = c(2013, 2014), Value = 4:3))

library(reshape2)
library(ggplot2)

# melt list to ggplot-friendly data frame in long format
df <- melt(mylist, id.var = "Year")

# plot, with color mapped to the original list elements
ggplot(data = df, aes(x = Year, y = value, color = L1, group = L1)) +
  geom_point() +
  geom_line()
  # + theme_xxx() + scale_color_yyy() of your choice