【问题标题】:two scatterplots in ggplot with arrows between the two scatterplotsggplot 中的两个散点图,两个散点图之间有箭头
【发布时间】:2014-02-20 06:02:47
【问题描述】:

我知道put two scatterplots onto one plot in ggplot2是可以的,但是我还需要在相关点之间放一个箭头。

例如,如果我有以下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)

我知道我可以像这样让两个散点图出现在同一个图上:

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()

产生:

但我想要的是这样的:

也就是说,我想要从MB_F1_ONGLIDE 值到MB_F1_OFFGLIDE 值的箭头,以及从MB_F2_ONGLIDE 值到MB_F2_OFFGLIDE 值的箭头。

这可能吗?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    是的 - 您可以使用 geom_segment(),您需要加载 gridgridExtra 来绘制箭头。您可以在 geom_segment 调用中修改线条大小/颜色,查看 ?arrow 了解如何更改箭头形状/行为

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2016-10-09
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多