4

Suppose I have a data frame with records (Value) for 100 subjects (Subject), which were measured with three different methods (Method). Now I would like to plot Value from each method against each other, so in this case "base-new", "base-edge" and "new-edge". How can I do this in ggplot2 based on a single numeric variable using facet_wrap?

dummy <- data.frame(Value = c(rnorm(100, mean = 35, sd = 2),
                              rnorm(100, mean = 47, sd = 2),
                              rnorm(100, mean = 28, sd = 1)),
                    Method = c(rep("base", times = 100),
                               rep("new", times = 100),
                               rep("edge", times = 100)),
                    Subject = rep(paste0("M", seq_len(100)), times = 3))
str(dummy)

## 'data.frame':    300 obs. of  3 variables:
##  $ Value  : num  32.9 32.2 37 36.6 33 ...
##  $ Method : Factor w/ 3 levels "base","edge",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Subject: Factor w/ 100 levels "M1","M10","M100",..: 1 13 24 35 46 57 68 79 90 2 ...

This code does not work and is just meant as an illustration for what I would like to do:

library("ggplot2")
ggplot(dummy, aes(Value)) +
    geom_point() +
    facet_wrap(~ Method)

Edit

This would be my solution using base R:

opar <- par()
par(mfrow = c(1, 3))
plot(dummy[dummy$Method == "base", "Value"],
     dummy[dummy$Method == "new", "Value"],
     xlab = "base", ylab = "new")
plot(dummy[dummy$Method == "base", "Value"],
     dummy[dummy$Method == "edge", "Value"],
     xlab = "base", ylab = "edge")
plot(dummy[dummy$Method == "new", "Value"],
     dummy[dummy$Method == "edge", "Value"],
     xlab = "new", ylab = "edge")
par(opar)

Base R Approach

matthias
  • 164
  • 1
  • 10
  • Can you explain more on what you mean by "agains each other using `facet_wrap`? I can't picture that in my head. Maybe you just need `ggplot(dummy, aes(Subject, Value)) +geom_point() +facet_wrap(~ Method)`? – David Arenburg Aug 12 '15 at 10:04
  • 1
    I have added an example for the plot using base R as an illustration of what I would like to achieve. – matthias Aug 12 '15 at 10:19
  • I think the most straightforward way is to restructure your data into columns `x`, `y` and `method_combo` where the latter would be e.g.new_base – mts Aug 12 '15 at 10:22
  • @mts I think that I can see what you imagine, but that would leave me with a new problem, namely labelling the axes for all facets, wouldn't it? – matthias Aug 12 '15 at 10:28
  • @mattw true, I suggested a workaround in my answer below, I'm not sure it fits your needs. – mts Aug 12 '15 at 11:01

1 Answers1

1

So while this isn't exactly what you were looking for it comes close: I suggest a matrix of plots with facet_grid instead:

Your data needs a slightly different format:

set.seed(1234)
dummy <- data.frame(Value = c(rnorm(100, mean = 35, sd = 2),
                              rnorm(100, mean = 47, sd = 2),
                              rnorm(100, mean = 28, sd = 1)),
                    Method = c(rep("base", times = 100),
                               rep("new", times = 100),
                               rep("edge", times = 100)),
                    Subject = rep(paste0("M", seq_len(100)), times = 3))
dummy2 = rbind(cbind.data.frame(x = dummy$Value[1:100], xmet = rep("base", 100), y = dummy$Value[101:200], ymet = rep("new", 100)),
               cbind.data.frame(x = dummy$Value[1:100], xmet = rep("base", 100), y = dummy$Value[201:300], ymet = rep("edge", 100)),
               cbind.data.frame(x = dummy$Value[101:200], xmet = rep("new", 100), y = dummy$Value[201:300], ymet = rep("edge", 100)))

And your plotting is done with:

library("ggplot2")
ggplot(dummy2, aes(x = x, y = y)) +
  geom_point() +
  facet_grid(ymet ~ xmet)

Which gives:

enter image description here

Now you could add e.g. a legend in the free field. My starting point was an answer to this question of mine

Community
  • 1
  • 1
mts
  • 2,160
  • 2
  • 24
  • 34