tomatofrommars
tomatofrommars

Reputation: 11

Naming multiple plots based on columns of dataframes

I have a list of dataframes (dflist) and data I want to compare each data frame against (actual)

A sample of what the data looks like:

    dflist[['2Source'}}
    Chem 1 Chem 2 
    .01     .02
    .02     .03
    .01     .03

    actual
  Chem 1 Chem 2 
    .01     .02
    .03     .02
    .01     .04

I want to plot each column against the identical column in the other table so, dflist[['2Source']][Chem 1] vs actual[Chem 1] and for chem 2. My actual data has 18 chemicals so I'm trying to write something with lapply to look all of them.

par(mfrow=c(3,4))

Chemnames <- names(dflist[['2Source']])

baseplot <- function(x,y, name) {
  plot(x, y, main = name)
  abline(lm(y~x))}

lapply(seq_along(dflist), function(i)
  mapply(function(x,y, name) {
    plot(x, y, main = names(Chemnames(name)))
    abline(lm(y~x))}, x = actual, y = dflist[[i]]))

This is my current code which seems to work minus the labels and I can't figure it out.

Upvotes: 0

Views: 64

Answers (1)

stefan
stefan

Reputation: 124213

Maybe this is what you are looking for:

dflist <- list()

dflist$`2Source` <- data.frame(
  `Chem 1` = c(.01, .02, .01), 
  `Chem 2` = c(.02, .03, .02))

actual <- data.frame(
  `Chem 1` = c(.01, .03, .01), 
  `Chem 2` = c(.02, .02, .04))

par(mfrow=c(3,4))

baseplot <- function(x, y, name) {
  plot(x, y, main = name)
  abline(lm(y ~ x))
}

# Just to check that we get the correct plots
baseplot(actual$Chem.1, dflist$`2Source`$Chem.1, "Chem.1")
baseplot(actual$Chem.2, dflist$`2Source`$Chem.2, "Chem.2")
# Now with lapply
lapply(dflist, function(df) mapply(baseplot, x = actual, y = df, name = names(df)))
#> $`2Source`
#> $`2Source`$Chem.1
#> NULL
#> 
#> $`2Source`$Chem.2
#> NULL

Upvotes: 1

Related Questions