Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange lines with ggplot2

Tags:

r

ggplot2

I have the following dataset:

> str(dat)
'data.frame':   5000 obs. of  3 variables:
 $ y: num  0.864 0.869 0.871 0.879 0.874 0.871 0.871 0.873 0.864 0.869 ...
 $ A: Factor w/ 5 levels "0.2","0.5","0.8",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ x: num  1 2 3 4 5 6 7 8 9 10 ...
> head(dat)
      y   A x
1 0.864 0.2 1
2 0.869 0.2 2
3 0.871 0.2 3
4 0.879 0.2 4
5 0.874 0.2 5
6 0.871 0.2 6

The "x" column is the vector c(1:5000):

> all(dat$x==1:5000)
[1] TRUE

Hence I don't understand the presence of some lines when drawing the following plot:

ggplot() + geom_line(aes(x=x, y=y, color=A), data=dat) 

The lines I'm talking about are indicated by the three black arrows in the figure: ggplot

EDIT: below is a similar example with a reproducible simulated dataset:

set.seed(666)
mu <- rep(c(200, 400, 600, 300, 500), each=1000)
A <- factor(rep(c(1,2,3,1,2), each=1000))
y <- rnorm(length(mu), mu, 100)
dat <- data.frame(x=1:length(mu), y=y, A=A)
ggplot() + geom_line(aes(x=x, y=y, color=A), data=dat)
like image 233
Stéphane Laurent Avatar asked May 10 '26 19:05

Stéphane Laurent


1 Answers

You need another variable in your data frame to denote the different blocks (i.e. regions with the same color) and the group argument in geom_line:

dat <- data.frame(x=1:length(mu), y=y, A=A, B=gl(5, 1000))
ggplot() + geom_line(aes(x=x, y=y, color=A, group=B), data=dat)

ggplot output

like image 200
rcs Avatar answered May 15 '26 04:05

rcs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!