Hugh Warden
Hugh Warden

Reputation: 446

Plotting a facet grid in R using ggplot2 with only one variable

I have a data frame, called mouse.data, with 3 columns: Eigenvalues, DualEigenvalues and Experiment. This question does not concern the DualEigenvalues data, so that can be forgotten.

We ran 5 experiments and used the data from each experiment to calculate 14 eigenvalues. So the first 14 rows of this data frame are the 14 eigenvalues of the first experiment, with the experiment entry having value 1, the second 14 rows are the 14 eigenvalues of the second experiment with the experiment entry having value 2 etc.

I am then plotting the eigenvalues of each pairwise experiment against each other, here is an example of this code:

eigen.1 <- mouse.data$Eigenvalues[mouse.data$Experiment == 1]
eigen.2 <- mouse.data$Eigenvalues[mouse.data$Experiment == 2]
p.data <- data.frame(x = eigen.1, y = eigen.2)
ggplot(p.data, aes(x,y)) + geom_abline(slope = 1, colour = "red") + geom_point()

This gives me graph like this one:

Plot Example

This is precisely what I want this graph to look like.

What I would like to do, but can't work out, is to plot a facet_grid so that the plot in the ith row and jth column plots the eigenvalues from the ith experiment on the y-axis and the eigenvalues from the jth experiment on the x-axis.

This is the closest I have got so far, I hope this makes it clearer what I mean.

Facet Example

Upvotes: 0

Views: 768

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174468

This is tricky without a reproducible example of your data, but it sounds like we can roughly approximate the structure of your data frame like this:

library(ggplot2)

set.seed(1)

Eigen <- as.vector(sapply(runif(5, .5, 1.5), 
                          function(x) sort(rgamma(14, 2, 0.02*x))))

mouse.data <- data.frame(Experiment = rep(seq(5), each = 14), Eigenvalue = Eigen)

head(mouse.data)
#>   Experiment Eigenvalue
#> 1          1   39.61451
#> 2          1   44.48163
#> 3          1   54.57964
#> 4          1   75.06725
#> 5          1   75.50014
#> 6          1   94.41255

The key to getting the plot to work is to reshape your data into a long-format data frame that contains each combination of experiments. One way to do this is to split the data frame by Experiment, then use simple indexing of the resultant list (using rep) to get all unique pairs of data frames. Each unique pair is stuck together column-wise, then the resultant 25 data frames are all joined row-wise into the plotting data frame.

experiments <- split(mouse.data, mouse.data$Experiment)

experiments <- mapply(cbind, 
                      experiments[rep(1:5, 5)], 
                      experiments[rep(1:5, each = 5)], 
                      SIMPLIFY = FALSE)

p.data <- do.call(rbind, lapply(experiments, setNames, 
                                nm = c("Experiment1", "x",
                                       "Experiment2", "y")))

Once we have done this, we can use your plot code, with the addition of a facet_grid call:

ggplot(p.data, aes(x,y)) + 
  geom_abline(slope = 1, colour = "red") + 
  geom_point() +
  facet_grid(Experiment1~Experiment2)

enter image description here

Upvotes: 1

Related Questions