Ggplot2: multiple plots with a box and labels around them

I am trying to create a plot grid that needs to have a rectangle, checkerboard markings (i.e. letters top / bottom, numbers on the sides), and a vertical line separating the two halves of the grid.

I was hoping to do this (or something similar) using faceting, but facet_grid does not create side marks (like here ) when setting a lot of plots, it appears:

testdata <- cbind("ID"=c(rep(1:24, each=10)), 
                  "TT"=c(rep(0:1, each=5)), 
                  "RD"=c(seq(1:5)), 
                  "DV"=rnorm(240, 10, 5))
testdata <- data.frame(testdata)

testplot <- ggplot(data=testdata, aes(x=RD, y=DV)) + 
  geom_line(colour="black") + geom_point() +
  facet_wrap(TT ~ ID, ncol=4)

      

enter image description here

(Another problem with facet_wrap is that it doesn't fit the ID ~ TT ordering, which I would prefer, displaying these values ​​as [1,0], [1,1], [2,0], [2,1 ] rather than the preferred ones [1.0], [2.0], [3.0], [1.1], [2.1] ... but that could be worked around.)

Another option I have considered is using box () ; however this only works with individual plots (creating an object with grid.arrange / organizGrob does not work).

Ultimately, I need a plot something like the one below. I would appreciate any suggestions!

Example

+3


source to share


2 answers


It seems pretty close.

set.seed(1)   # for reproducible example
testdata <- data.frame(ID=rep(LETTERS[1:4], each=60), 
                       TT=rep(1:6, each=10), 
                       grp=rep(0:1,each=5),
                       RD=seq(1:5), 
                       DV=rnorm(240, 10, 5))
library(ggplot2)
library(gridExtra)
ggp <- ggplot(data=testdata, aes(x=RD, y=DV, group=grp)) + 
  geom_line(colour="black") + geom_point() +
  facet_grid(TT ~ ID)+
  theme(strip.background=element_rect(fill=NA),
        strip.text=element_text(color="red",face="bold",size=15),
        strip.text.y=element_text(angle=0))
grid.newpage()
grid.draw(arrangeGrob(ggp))
grid.rect(x=0.51,y=0.5,width=0.007,height=0.8,gp=gpar(col=NA,fill="red"))

      

I added a variable grp

to your example because you feel like you need two lines in each panel.



We can get most of the path with facet_grid(...)

in ggplot

, plus theme(...)

to make the labels bold and red and turn off the stripe background (faceted label).

To add a vertical line is unfortunately required to be used grid.rect(...)

in a package gridExtra

. As you can see, there is some tweak to the location of the rectangle to get this right. You seem to need to do this if you have more than four categories all over the top.

AFAIK there is no easy way to place the vertical facet labels on the left.

As a sidebar, note that you don't need, for example c(rep(...))

. rep(...)

already returns a vector. Also, and this is important, it is really a bad idea to use data.frame(cbind(...))

. It just data.frame(...)

does the same thing, much faster and doesn't create an intermediate matrix.

+4


source


To make my comments clearer.

I would recommend creating a dataframe that looks like this:

ID  TT  RD  DV          ColumnLetter     RowNumber
1   0   1   12.4734172       A               1
1   0   2   7.1034136        B               2
1   0   3   3.0688039        C               3
1   0   4   -2.4502442       D               4

      



Then use

face_grid(RowNumber~ColumnLetter)

      

for formatting as desired.

0


source







All Articles