可视化重叠和非重叠范围

时间:2016-10-19 17:07:56

标签: r ggplot2 data-visualization

我正在处理重叠范围的某些扁平化,并希望通过以下方式可视化初始数据(重叠)和结果集(展平):

初始数据: enter image description here

结果集 enter image description here

R是否可以,例如ggplot2?

2 个答案:

答案 0 :(得分:2)

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

enter image description here

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

答案 1 :(得分:1)

The answer to the second part of the question can be using @hrbrmstr 's great answer for the first part. We can use overplotting to our advantage and simply set the y coordinates for the segments to a fixed value (for example 1, which where "red" is):

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)