简体   繁体   中英

ggplot - Graphing items from a list without for loops

I am trying to plot a few graphs from a list which looks as follows:

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

I would like to make a plot as below for each item in the list, ie for each company, for example the plot for company A would look like:

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()

Now I can create a for loop to go through each element of the list and create the plot as above. However, my actual data set has thousands of companies so I would ideally like to create a dataset which can be used to plot the required data as above for each company.

Any help appreciated.

edit: I've got to the following:

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)) 

However I get these errors when plotting:

Warning in 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)), : There are other near singularities as well. 1.0201

Any help appreciated

You could first make a data.frame out of your list, using column- and rownames, and reshape it into long format. Then with facet_grid() you can get the two plots.

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]))

Yielding 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

Plot

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))

情节

Data

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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