我正在尝试制作一个特定的情节来可视化我的数据.它由一个ID和4个值组成,第一个值应该根据值在x轴上移动,第二个和第三个是间隔的开始和结束,第四个值只是一个值的一部分数据点的位置应该与其他点对齐.我用油漆画了一张照片来展示我想要的东西:
这是相应的数据:
id <- c(1,2,3,4,5,6)
v1 <- c(3,4,3,6,5,1)
v2 <- c(5,6,6,9,8,4)
v3 <- c(10,12,12,15,12,13)
v4 <- c(1,2,1,1,4,3)
df <- data.frame(id,v1,v2,v3,v4)
id v1 v2 v3 v4
1 1 3 5 10 1
2 2 4 6 12 2
3 3 3 6 12 1
4 4 6 9 15 1
5 5 5 8 12 4
6 6 1 4 13 3
Run Code Online (Sandbox Code Playgroud)
我和ggplot2很熟悉,间隔看起来像置信区间,所以也许我可以做点什么呢?非常感谢!
你需要的组合geom_point,geom_segment和geom_text:
ggplot(df) +
geom_point(aes(x=id,y=v1), size=4, color="red") +
geom_segment(aes(x=id, xend=id, y=v2, yend=v3), size=2) +
geom_text(aes(x=id, y=16, label=v4)) +
scale_x_continuous(breaks=id) +
coord_flip() +
theme_bw()
Run Code Online (Sandbox Code Playgroud)
这使:
另一个选择是使用geom_errorbar而不是geom_segment:
ggplot(df) +
geom_point(aes(x=id,y=v1), size=4, color="red") +
geom_errorbar(aes(x=id, ymin=v2, ymax=v3), size=2, width=0.2) +
geom_text(aes(x=id, y=16, label=v4)) +
scale_x_continuous(breaks=id) +
coord_flip() +
theme_bw()
Run Code Online (Sandbox Code Playgroud)
这导致: