ggplot-在没有for循环的情况下从列表中绘制项目

时间:2018-07-08 16:08:45

标签: r list ggplot2

我正在尝试从列表中绘制一些图形,如下所示:

rs1 <- data.frame(c("A1", "A2", "A3", "A4", "A5"), runif(5), runif(5), runif(5))
names(rs1) <- c("column", 2007, 2008, 2009)
rownames(rs1) <- rs1$column
rs1 <- rs1[2:4]

rs2 <- data.frame(c("A1", "A2", "A3", "A4", "A5"), runif(5), runif(5), runif(5))
names(rs2) <- c("column", 2007,2008,2009)
rownames(rs2) <- rs2$column
rs2 <- rs2[2:4]


l1 <- list(rs1, rs2)
names(l1) <- c("CompanyA", "CompanyB")
l1

$CompanyA
        2007      2008      2009
A1 0.6486099 0.5310441 0.7095210
A2 0.3881914 0.2394723 0.6997068
A3 0.4295121 0.6618062 0.2725275
A4 0.6548886 0.3708362 0.3510994
A5 0.3825162 0.7545498 0.3470293

$CompanyB
         2007       2008       2009
A1 0.96786369 0.95582327 0.12709719
A2 0.69906099 0.22706448 0.47463029
A3 0.67795904 0.07207512 0.07862240
A4 0.09535442 0.74312544 0.19818115
A5 0.36331461 0.98673541 0.07754267

我想为列表中的每个项目(即每个公司)绘制如下图,例如,公司A的图如下所示:

compA <- l1[['CompanyA']]

compA      <- as.data.frame(t(compA))
compA$year <- rownames(compA)
compA      <- melt(compA, id.vars=c("year"))

ggplot(compA, aes(x=as.numeric(year), y=value, color=variable)) + geom_line()

现在,我可以创建一个for循环来遍历列表的每个元素,并如上所述创建图表。但是,我的实际数据集有数千个公司,因此理想情况下,我想创建一个数据集,该数据集可用于绘制上述每个公司所需的数据。

任何帮助表示赞赏。

编辑: 我要注意以下几点:

main.df <- data.frame()
for (nm in names(l1)){
  df <- l1[[nm]]
  df      <- as.data.frame(t(df))
  df$year <- rownames(df)
  df <- melt(df, id.vars=c("year"))
  df$Company <- nm
  main.df <- rbind(df, main.df)
}

ggplot(data = main.df) +
  geom_smooth(mapping = aes(x=as.numeric(year), y = value, color = variable)) 

但是在绘制时出现这些错误:

  

predLoess中的警告(object $ y,object $ x,newx = if(is.null(newdata))object $ x else if(is.data.frame(newdata))   as.matrix(model.frame(delete.response(terms(object)),:         还有其他附近的奇点。 1.0201

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

您可以首先使用列名和行名在列表中制作一个data.frame,然后将其整形为长格式。然后使用facet_grid()可以得到两个图。

l1[[1]][4] <- names(l1)[1]
l1[[1]][5] <- rownames(l1[[1]])
l1[[2]][4] <- names(l1)[2]
l1[[2]][5] <- rownames(l1[[2]])

df1 <- do.call(rbind, l1)
rownames(df1) <- NULL
names(df1)[4:5] <- c("company", "column")

df1 <- reshape(df1, direction = "long", varying = list(names(df1)[1:3]), v.names = "value", 
        idvar = c("company", "column"), timevar = "year", times=names(df1[1:3]))

产生df1

> head(df1, 10)
                  company column year      value
CompanyA.A1.2007 CompanyA     A1 2007 0.19281213
CompanyA.A2.2007 CompanyA     A2 2007 0.05178135
CompanyA.A3.2007 CompanyA     A3 2007 0.15578975
CompanyA.A4.2007 CompanyA     A4 2007 0.70182986
CompanyA.A5.2007 CompanyA     A5 2007 0.94615300
CompanyB.A1.2007 CompanyB     A1 2007 0.19962672
CompanyB.A2.2007 CompanyB     A2 2007 0.14236462
CompanyB.A3.2007 CompanyB     A3 2007 0.60083513
CompanyB.A4.2007 CompanyB     A4 2007 0.47985951
CompanyB.A5.2007 CompanyB     A5 2007 0.02689391

情节

ggplot(df1, aes(x=as.factor(year), y=value, color=column, group=column)) +
  geom_line() +
  labs(x="Year") +
  facet_grid(company ~ .) +
  scale_x_discrete(expand=c(.1, .1))

https://developer.android.com/google/play/billing/billing_java_kotlin

数据

setseed(42)
rs1 <- data.frame(c("A1", "A2", "A3", "A4", "A5"), runif(5), runif(5), runif(5))
names(rs1) <- c("column", 2007, 2008, 2009)
rownames(rs1) <- rs1$column
rs1 <- rs1[2:4]

rs2 <- data.frame(c("A1", "A2", "A3", "A4", "A5"), runif(5), runif(5), runif(5))
names(rs2) <- c("column", 2007, 2008, 2009)
rownames(rs2) <- rs2$column
rs2 <- rs2[2:4]

l1 <- list(rs1, rs2)