Emilie Picard-Cantin
Emilie Picard-Cantin

Reputation: 290

How to draw all plots of a data frame in R?

I have a data frame representing a benchmark and I would like to produce all possible comparison plots. Here is a small example of data frame that represents my problem.

 df = data.frame("A"=c(1,2,3,1,2,3,1,2,3,1,2,3), "B"=c(1,1,1,2,2,2,1,1,1,2,2,2), "C"=c(1,1,1,1,1,1,2,2,2,2,2,2), "D"=c(4,5,6,7,8,9,10,11,12,13,14,15))

enter image description here

I want to produce the following plots.

Is there a simple way to this in R ?

For now, I don't mind that they are in different plots or not. Any representation would be ok at this point. I only need all plots to be produced, since I don't know how we want to display our results.

Edit

I forgot to specify in my example that the columns of the data frame do not have the same factor levels. Here is a more complete example.

df = data.frame("A"=c(1,2,3,1,2,3,1,2,3,1,2,3), 
            "B"=c("[0,1]","[0,1]","[0,1]","[1,3]","[1,3]","[1,3]","[0,1]","[0,1]","[0,1]","[1,3]","[1,3]","[1,3]"), 
            "C"=c(1,1,1,1,1,1,2,2,2,2,2,2), 
            "D"=c(4,5,6,7,8,9,10,11,12,13,14,15))

Using @mattek's solution, I have the following plots. enter image description here

This is great. If I could remove the extra values from the x-axis and keep only the corresponding factors for each column, that would be perfect.

Upvotes: 0

Views: 271

Answers (4)

mattek
mattek

Reputation: 942

library(ggplot2)
library(reshape2)

First, we melt your table:

df.plot = melt(df, 
               measure.vars = c('A', 'B', 'C'), 
               id.vars = 'D', 
               variable.name = 'var.name', 
               value.name = 'val.abc')

Then, we add groupings column:

df.plot$grouping = rep(1:4, 3, each = 3)

And we are ready to plot:

ggplot(df.plot, aes(x = val.abc, y = D, group = as.factor(grouping))) +
  facet_wrap(~ var.name) +
  geom_line(aes(colour = var.name)) +
  geom_point(aes(colour = var.name))

Using facet_wrap(~ var.name, scale = "free_x") instead would get rid of non-existant factors in every facet.

enter image description here

Upvotes: 1

bouncyball
bouncyball

Reputation: 10761

Here's what I would do, I would create three new variables which capture the different combinations of A, B, and C fixed:

library(dplyr)
library(ggplot2)
dat <- data.frame("A"=c(1,2,3,1,2,3,1,2,3,1,2,3), 
                  "B"=c(1,1,1,2,2,2,1,1,1,2,2,2), 
                  "C"=c(1,1,1,1,1,1,2,2,2,2,2,2), 
                  "D"=c(4,5,6,7,8,9,10,11,12,13,14,15))

# add variables for A-B, A-C, B-C
dat <- dat %>%
  mutate('A - B' = paste(A, '-', B),
         'A - C' = paste(A, '-', C),
         'B - C' = paste(B, '-', C))

Then we make the plots:

ggplot(dat, aes(y = D))+
  geom_line(aes(x = C, colour = `A - B`))

enter image description here

ggplot(dat, aes(y = D))+
  geom_line(aes(x = B, colour = `A - C`))

enter image description here

ggplot(dat, aes(y = D))+
  geom_line(aes(x = A, colour = `B - C`))

enter image description here

Upvotes: 0

Derek Corcoran
Derek Corcoran

Reputation: 4082

Another option comes from ggplot using the GGaly package:

library(ggplot2)
library(GGally)

this helps a lot if some of your data is a factor, using your data, lets assume that A is a factor variables

df = data.frame("A"=as.factor(c(1,2,3,1,2,3,1,2,3,1,2,3)), "B"=c(1,1,1,2,2,2,1,1,1,2,2,2), "C"=c(1,1,1,1,1,1,2,2,2,2,2,2), "D"=c(4,5,6,7,8,9,10,11,12,13,14,15))

then ggpairs would make boxplots instead of points, you can choose there

enter image description here ggpairs(df)

Upvotes: 0

Derek Corcoran
Derek Corcoran

Reputation: 4082

Possible answer for exploratory analysis that will show correlation between variables and also a smoothing line:

df = data.frame("A"=c(1,2,3,1,2,3,1,2,3,1,2,3), "B"=c(1,1,1,2,2,2,1,1,1,2,2,2), "C"=c(1,1,1,1,1,1,2,2,2,2,2,2), "D"=c(4,5,6,7,8,9,10,11,12,13,14,15))

panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
{
  usr <- par("usr"); on.exit(par(usr))
  par(usr = c(0, 1, 0, 1))
  r <- cor(x, y)
  txt <- format(c(r, 0.123456789), digits = digits)[1]
  txt <- paste0(prefix, txt)
  if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
  text(0.5, 0.5, txt, cex = cex.cor * r)
}

pairs(df, lower.panel = panel.smooth, upper.panel = panel.cor)

enter image description here

Upvotes: 1

Related Questions