【发布时间】:2016-10-21 02:07:47
【问题描述】:
【问题讨论】:
标签: r ggplot2 data-visualization
【问题讨论】:
标签: r ggplot2 data-visualization
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 标签(我们无法为您完成所有工作;-)
【讨论】:
问题第二部分的答案可以使用@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)
【讨论】: