Reputation: 1511
I'm trying to make a specific plot to visualize my data. It consists of an ID and 4 values, the first is a value that should be shifted on the x-axis depending on the value, the second and third are the beginning and end of an interval and the fourth is just a value that is part of the data point and should be at the end aligned with the others. I made a picture in paint to show what I'm trying to acomplish:
Here is the corresponding data:
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
I'm familair with ggplot2, the intervals look like confidence intervals so maybe I can do something with that? Thanks a lot!
Upvotes: 1
Views: 194
Reputation: 83255
You need a combination of geom_point
, geom_segment
and 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()
which gives:
Another option is using geom_errorbar
instead of 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()
this results in:
Upvotes: 7