I'm looking to plot something similar to this in R. Can this be this be done with ggplot or some other package?

Found on the following blog:
http://intelligenttradingtech.blogspot.com/2011/07/pattern-recognition-forward-boxplot.html
Here is how to construct the graph using ggplot2.

I constructed the data manually, by specifying the coordinates of each line start and end position. An improvement would obviously be to automate this using an algorithm. Since this wasn't the question, I didn't attempt to solve this too.
Create the data:
arrowdata <- c(
0, 0, 1, 0,
1, 1, 2, 1,
1, -1, 2, -1,
2, 1.5, 3, 1.5,
2, 0.5, 3, 0.5
)
linesdata <- c(
1, 0, 1, 1,
1, 0, 1, -1,
2, 1, 2, 1.5,
2, 1, 2, 0.5
)
labeldata <- data.frame(
x = c(0.5, 1.5, 2.5),
y = c(0, 1, 1.5),
labels=c("Label 1", "Label2", "Label 3")
)
adat <- as.data.frame(matrix(arrowdata, ncol=4, byrow=TRUE))
ldat <- as.data.frame(matrix(linesdata, ncol=4, byrow=TRUE))
Load the ggplot2 and grid packages, then plot:
library(ggplot2)
library(grid) # For arrow() function
ggplot() +
geom_segment(
data=adat,
aes(x=V1, y=V2, xend=V3, yend=V4),
arrow=arrow(length = unit(0.05, "npc"), type="closed"),
col="blue"
) +
geom_segment(
data=ldat,
aes(x=V1, y=V2, xend=V3, yend=V4),
col="blue"
) +
geom_text(data=labeldata, aes(x, y, label=labels),
size=8, vjust=-0.2, col="blue"
) +
theme_bw() +
opts(
axis.text.x=theme_blank(),
axis.text.y=theme_blank(),
axis.ticks=theme_blank(),
axis.title.x=theme_blank(),
axis.title.y=theme_blank(),
panel.grid.major=theme_blank(),
panel.border=theme_blank()
) +
coord_cartesian(ylim=c(-1.5, 2)) # Create some additional space for labels
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With