Search code examples
rggplot2rectfacet-wrapannotate

ggplot - using annotate across facets


This is a basic question but haven't been able to find the answer on here. I am creating a figure with ggplot from the following (overly simplified) data:

df.for.graph <- setNames(data.frame(matrix(ncol = 5,nrow = 8)), c("xp","yp","loc","cong","emotion"))
df.for.graph$xp <- c(948.7, 977.2, 1023.4, 953.3, 979.4,936.3, 911.6,877.2)
df.for.graph$yp <- c(923.0, 893.0, 294.9, 241.5, 898.6, 960.9, 154.4, 263.4)
df.for.graph$loc <- as.factor(c("Bottom", "Bottom", "Top", "Top", "Bottom", "Bottom", "Top", "Top"))
df.for.graph$cong <- as.factor(c("Incongruent","Congruent","Incongruent","Congruent", "Incongruent","Congruent","Incongruent","Congruent"))
    df.for.graph$emotion <- as.factor(c("Angry", "Angry", "Angry", "Angry", "Happy","Happy", "Happy","Happy"))

My call to ggplot is as follows:

ggplot(df.for.graph,aes(x=xp,y=yp,color=loc,shape=cong)) +
  geom_point() +
  scale_color_manual(values=c("red","blue")) +
  scale_shape_manual(values=c(1,4)) +
  scale_fill_manual(values=c("green", "yellow")) +
  scale_x_continuous(breaks = seq(from = 0, to = 1920, by = 160), limits=c(0,1920)) +
  scale_y_reverse(breaks = seq(from = 0, to = 1200, by = 80), limits=c(1200,0)) +
  labs(shape = "Congruence", color = "Probe Location",x = "X Position", y = "Y Position") +
  facet_wrap(vars(emotion),nrow=2,ncol=1) +
  theme(axis.title.x = element_text(face="bold",size=20),
        axis.text.x = element_text(face="bold",size=15, color="black"),
        axis.title.y = element_text(face="bold",size=20),
        axis.text.y = element_text(face="bold",size=15, color="black"),
        panel.background = element_rect(fill="white"),
        panel.border = element_rect(colour = "black", fill=NA, size=2),
        strip.text = element_text(face="bold",size=20),
        legend.text = element_text(colour = "black", size=15),
        legend.title = element_text(colour = "black", size=15)) +
  annotate("rect",xmin=0, xmax=1920, ymin=0, ymax=599,alpha=.4) +
  annotate("rect",xmin=0, xmax=1920, ymin=602, ymax=1200,alpha=.4)

This results in the following: enter image description here

However I want the call to annotate to leave a line between the two rectangles on both facets of the plot. Currently it only leaves a line between the two on the top (Angry) facet. I thought that supplying the rect coordinates without specifying facets should draw the same two rectangles on each facet of the plot...

Any thoughts on how to make the bottom facet look like the top one?

Thanks in advance!


Solution

  • It's a matter of it displaying it properly on the device, i suggest you save it to a png or pdf. First save plot as object:

    g1 = ggplot(df.for.graph,aes(x=xp,y=yp,color=loc,shape=cong)) +
      geom_point() +
      scale_color_manual(values=c("red","blue")) +
      scale_shape_manual(values=c(1,4)) +
      scale_fill_manual(values=c("green", "yellow")) +
      scale_x_continuous(breaks = seq(from = 0, to = 1920, by = 160), limits=c(0,1920)) +
      scale_y_reverse(breaks = seq(from = 0, to = 1200, by = 80), limits=c(1200,0)) +
      labs(shape = "Congruence", color = "Probe Location",x = "X Position", y = "Y Position") +
      facet_wrap(vars(emotion),nrow=2,ncol=1) +
      theme(axis.title.x = element_text(face="bold",size=20),
            axis.text.x = element_text(face="bold",size=15, color="black"),
            axis.title.y = element_text(face="bold",size=20),
            axis.text.y = element_text(face="bold",size=15, color="black"),
            panel.background = element_rect(fill="white"),
            panel.border = element_rect(colour = "black", fill=NA, size=2),
            strip.text = element_text(face="bold",size=20),
            legend.text = element_text(colour = "black", size=15),
            legend.title = element_text(colour = "black", size=15)) +
      annotate("rect",xmin=0, xmax=1920, ymin=0, ymax=599,alpha=.4) +
      annotate("rect",xmin=0, xmax=1920, ymin=602, ymax=1200,alpha=.4)
    

    And save:

    ggsave(g1,file="g1.png",width=12,height=12)
    

    enter image description here