R 可视化重叠和非重叠范围

R 可视化重叠和非重叠范围,r,ggplot2,data-visualization,R,Ggplot2,Data Visualization,我正在对重叠范围进行一些展平,并希望通过以下方式可视化初始数据(重叠)和结果集(展平): 初始数据: 结果集: 例如,R和ggplot2是否可以这样做?read.table(header=TRUE,sep=“,”,text=“color,start,end 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,

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

初始数据:

结果集:

例如,R和ggplot2是否可以这样做?

read.table(header=TRUE,sep=“,”,text=“color,start,end
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))
红色,12.5,13.8 蓝色,0.0,5.4 绿色,2.0,12.0 黄色,3.5,6.7 橙色,6.7,10.0“,stringsAsFactors=FALSE)->df 图书馆(GG2) df$color
read.table(header=TRUE,sep=“,”,text=“color,start,end
红色,12.5,13.8
蓝色,0.0,5.4
绿色,2.0,12.0
黄色,3.5,6.7
橙色,6.7,10.0“,stringsAsFactors=FALSE)->df
图书馆(GG2)

df$color问题第二部分的答案可以使用@hrbrmstr对第一部分的伟大回答。我们可以利用过度抽签的优势,简单地将线段的y坐标设置为固定值(例如
1
,其中“红色”为):


p问题第二部分的答案可以使用@hrbrmstr的精彩答案作为第一部分。我们可以利用过度抽签的优势,简单地将线段的y坐标设置为固定值(例如
1
,其中“红色”为):

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