Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add lines between points without "touching" the points, like in "type='b'"?

Tags:

plot

r

line

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.

like image 362
Antonin Avatar asked Dec 06 '22 21:12

Antonin


2 Answers

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()

enter image description here

like image 127
Chase Avatar answered Apr 07 '23 11:04

Chase


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")

enter image description here

like image 38
Andrie Avatar answered Apr 07 '23 11:04

Andrie