I would like to add lines between points in a plot in R. But not between all of them.
So I use "lines". But I would like to keep the "type='b'" style, with the line stopping just before the point.
If ggplot is your thing, give this a whirl. ggplot doesn't natively support the type = "b"
as in base graphics. We can get around that though with some overplotting and subsetting:
library(ggplot2)
x <- seq(1, pi, pi/36)
y <- sin(x)
z <- data.frame(x,y)
ggplot(z, aes(x,y)) +
geom_line(data = subset(z, x > 1.5 & x < 2.5)) +
geom_point(size = 6, colour = "white") +
geom_point(size = 3, colour = "black") +
theme_bw()
Set up some data
x <- seq(1, pi, pi/36)
y <- sin(x)
Create plot with all points
plot(x, y)
Add lines of type="b"
for some of the points:
lines(x[10:20], y[10:20], type="b")
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