Why do I get disordered points in R base while not in ggplot for the same dataset

Viewed 25

I expended some hours to try to know what's wrong with my data while I was plotting lines with R base 'plot', until I checked out ggplot and the results was I was expecting to be.

Now I am curious to understand why is 'plot' connecting different groups of points.

Reduced Reproducible Dataset

DF3 = read.table( text="
.session_id dayhour average
1   4319703577       0   47.43
2   4319703577       1   48.00
3   4319703577       2   48.52
4   4319703577       3   48.78
5   4319703577       4   48.13
6   4319703577       5   47.52
7   4319703577       6   44.40
8   4319703577       7   29.70
9   4319703577       8    6.70
10  4319703577       9    2.28
11  4319703577      10    1.07
12  4319703577      11    0.02
13  4319703577      15    0.57
14  4319703577      16    1.45
15  4319703577      17    0.68
16  4319703577      22    9.70
17  4319703577      23   39.67
18  5577150313       0   51.48
19  5577150313       1   52.60
20  5577150313       2   52.88
21  5577150313       3   52.10
22  5577150313       4   50.72
23  5577150313       5   33.45
24  5577150313       6    7.70
25  5577150313       7    1.92
26  5577150313       8    0.58
27  5577150313      10    0.50
28  5577150313      11    1.00
29  5577150313      12    1.93
30  5577150313      13    3.80
31  5577150313      14    2.33
32  5577150313      15    2.83
33  5577150313      16    3.00
34  5577150313      17    2.38
35  5577150313      18    2.00
36  5577150313      19    2.00
37  5577150313      20    3.50
38  5577150313      21   13.68
39  5577150313      22   37.38
40  5577150313      23   51.23", )

Reproducible Plots

### Plot Sleep Schedule
DF3 %>% 
  arrange( .session_id, dayhour ) %>%
  ggplot( aes( dayhour, average, col=.session_id ) ) + 
  geom_line()+
  geom_vline( xintercept = 20.5, linetype=2 ) +
  geom_vline( xintercept = 10.5, linetype=2 )

DF3 %>% 
  arrange( .session_id, dayhour ) %>% 
  with( plot( a$dayhour, a$average, col=.session_id, type = "b" ) )
  abline( v = 20.5, lty=2 )
  abline( v = 10.5, lty=2 )

Plot results

enter image description here enter image description here enter image description here enter image description here

EDIT

I found this dataset to be more reproducible. The code is only to show that R-base plot connects the dots of different colored lines.

data <- ggplot2::economics_long                     # US economic time series
data

data %>% 
  group_by( variable, wday = lubridate::wday( date ),  ) %>% 
  summarize( value  = value %>% sum %>% round(2) ) %>% 
  ggplot( aes( wday, value, col=variable ) ) + 
  geom_line()

plot.new()
data %>% 
  group_by( variable, wday = lubridate::wday( date ),  ) %>% 
  summarize( value  = value %>% sum %>% round(2) ) %>% 
  with( plot( wday, value, col=factor(variable), type = "b" ) )

enter image description here enter image description here

1 Answers

compare these :


DF3 %>% 
  arrange( .session_id, dayhour ) %>%
  ggplot( aes( dayhour, average, col=.session_id ,group=.session_id) ) + 
  geom_line()+
  geom_vline( xintercept = 20.5, linetype=2 ) +
  geom_vline( xintercept = 10.5, linetype=2 )

plot.new()
DF3 %>% 
  arrange( .session_id, dayhour ) %>% 
  with( 
    plot( dayhour, average, col=factor(.session_id), type = "b" ) )
abline( v = 20.5, lty=2 )
abline( v = 10.5, lty=2 )
Related