1

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!

2
  • The annotate rect lines show up for me (if the plot is big enough along vertical axis). If you expand the plot size does it show up? Or use different numbers for ymin and ymax (try 590 and 610 instead of 599 and 602)? Commented Mar 5, 2020 at 1:20
  • Ah, yeah if I change the ymin and ymax so the space is bigger it does show up! However, it still doesn't show up in the bottom facet if I keep the original values, even when I full screen the image. Weird, but I can move forward with the larger interval instead - thanks for the help Commented Mar 5, 2020 at 3:53

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I agree that would be my recommendation as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.