代码之家  ›  专栏  ›  技术社区  ›  Vasily A

如何用分类坐标微移几何线段

  •  0
  • Vasily A  · 技术社区  · 5 年前

    x 轴是数字,而 y 是分类的,我绘制了不同类型的对象-点、线段、文本标签等。示例数据集:

    require(data.table)
    dt1 <- fread('
    subject type start stop
      sbjA    U    10    20
      sbjA    D    15    25
      sbjB    U     0    10
      sbjB   pnt   25
      sbjC    D    30    40
      sbjC    U    30    50
    ', fill=T)
    
    ggplot(dt1, aes(x=start, y=subject)) + 
      geom_segment(data = dt1[type=='U',], aes(xend=stop,yend=subject), col='red', size=3, alpha=0.5)+
      geom_segment(data = dt1[type=='D',], aes(xend=stop,yend=subject), col='blue', size=3, alpha=0.5)+
      geom_point(data = dt1[type=='pnt',])
    

    what I have 我想要这个: what I want 我怎样才能做到这一点 ?

    1 回复  |  直到 5 年前
        1
  •  0
  •   mt1022    5 年前

    position_dodge ,但与 geom_linerange

    ggplot(dt1, aes(x=start, y=subject)) + 
        geom_linerange(data = dt1[type %in% c('U', 'D')],
            aes(xmin=start,xmax=stop, color = type),
            size=3, alpha=0.5, position = position_dodge(width = 0.2))+
        scale_color_manual(values = c('red', 'blue')) +
        geom_point(data = dt1[type=='pnt',])
    

    enter image description here

    geom_segment 位置\u闪避

    ggplot(dt1, aes(x=start, y=subject)) + 
        geom_segment(data = dt1[type %in% c('U', 'D')],
            aes(xend=stop,yend=subject, color = type),
            size=3, alpha=0.5, position = position_dodge(width = 0.2))+
        scale_color_manual(values = c('red', 'blue')) +
        geom_point(data = dt1[type=='pnt',])
    

    enter image description here

    推荐文章