2

I know that it is possible to put two scatterplots onto one plot in ggplot2, but I also need to put an arrow between the relevant points.

For example, if I have the following data.frame:

SPEAKER <- c("A","A","B","B")
VOWEL <- c("ej","ow","ej","ow")
MB_F1_ONGLIDE <- c(423.88,533.297,465.796,532.118)
MB_F2_ONGLIDE <- c(1847.428,962.485,1815.381,1058.883)
MB_F1_OFFGLIDE <- c(404.827,480.176,423.381,522.727)
MB_F2_OFFGLIDE <- c(1885.349,911.669,1887.392,971.168)
data <- data.frame(SPEAKER,VOWEL,MB_F1_ONGLIDE,MB_F2_ONGLIDE,MB_F1_OFFGLIDE,MB_F2_OFFGLIDE)

I know that I can make two scatterplots appear on the same plot like this:

plot <- ggplot(data,aes(x = MB_F2_ONGLIDE,y = MB_F1_ONGLIDE,color = SPEAKER,label = VOWEL)) +
    geom_text(aes(x = MB_F2_ONGLIDE,y = MB_F1_ONGLIDE)) + 
    geom_text(aes(x = MB_F2_OFFGLIDE,y = MB_F1_OFFGLIDE)) +
    scale_x_reverse() +
    scale_y_reverse()

Which produces:

enter image description here

But what I want is something like:

enter image description here

That is, I want something that draws an arrow from the MB_F1_ONGLIDE value to the MB_F1_OFFGLIDE value and something that draws an arrow from the MB_F2_ONGLIDE value to the MB_F2_OFFGLIDE value.

Is this possible?

Community
  • 1
  • 1
Adam Liter
  • 875
  • 2
  • 10
  • 30

1 Answers1

4

Yes - you can use geom_segment(), you will need to load grid or gridExtra to draw the arrow. You can tinker with line size/color in the geom_segment call, look at ?arrow for how to change the arrow shape/behaviour

require(gridExtra)

ggplot(data,aes(x = MB_F2_ONGLIDE,y = MB_F1_ONGLIDE,color = SPEAKER,label = VOWEL)) +
  geom_text(aes(x = MB_F2_ONGLIDE,y = MB_F1_ONGLIDE)) + 
  geom_text(aes(x = MB_F2_OFFGLIDE,y = MB_F1_OFFGLIDE)) +
  geom_segment(aes(x = MB_F2_ONGLIDE,y = MB_F1_ONGLIDE,xend = MB_F2_OFFGLIDE,yend = MB_F1_OFFGLIDE,color=SPEAKER),arrow=arrow()) +
  scale_x_reverse() +
  scale_y_reverse()

enter image description here

Troy
  • 8,581
  • 29
  • 32