【问题标题】:Visualize overlapping and non-overlapping ranges可视化重叠和非重叠范围
【发布时间】:2016-10-21 02:07:47
【问题描述】:

我正在对重叠范围进行一些展平,并希望通过以下方式可视化初始数据(重叠)和结果集(展平):

初始数据:

结果集:

R 和例如 ggplot2 是否可以做到这一点?

【问题讨论】:

    标签: r ggplot2 data-visualization


    【解决方案1】:
    read.table(header=TRUE, sep=",", text="color,start,end
    red,12.5,13.8
    blue,0.0,5.4
    green,2.0,12.0
    yellow,3.5,6.7
    orange,6.7,10.0", stringsAsFactors=FALSE) -> df
    
    library(ggplot2)
    
    df$color <- factor(df$color, levels=rev(df$color))
    
    ggplot(df) +
      geom_segment(aes(x=start, xend=end, y=color, yend=color, color=color), size=10) + 
      scale_x_continuous(expand=c(0,0)) +
      scale_color_identity() +
      labs(x=NULL, y=NULL) +
      theme_minimal() +
      theme(panel.grid=element_blank()) +
      theme(axis.text.x=element_blank()) +
      theme(plot.margin=margin(30,30,30,30))
    

    还有其他关于 SO 的帖子展示了如何获得您所展示的 y 标签(我们无法为您完成所有工作;-)

    【讨论】:

      【解决方案2】:

      问题第二部分的答案可以使用@hrbrmstr 的第一部分的最佳答案。我们可以利用重叠绘图来发挥我们的优势,只需将线段的 y 坐标设置为固定值(例如 1,其中“红色”是):

      p <- ggplot(df) + 
           geom_segment(aes(x=start, xend=end, color=color),
                        y=1, yend=1, size=10) +
           scale_x_continuous(expand=c(0,0)) + scale_color_identity() + 
           labs(x=NULL, y=NULL) + 
           theme_minimal() +theme(panel.grid=element_blank()) +
           theme(axis.text.x=element_blank()) +
           theme(plot.margin=margin(30,30,30,30))
      print(p)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-12
        • 1970-01-01
        • 2013-10-28
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 2014-05-11
        相关资源
        最近更新 更多