4

Here's a minimal working example:

nat <- structure(list(X = 1:336, value = c(56, 52, 48, 44, 43, 44, 37, 
36, 29, 27, 15, 22, 36, 22, 59, 53, 49, 45, 44, 44, 38, 37, 30, 
27, 16, 22, 35, 21, 61.1, 54.6, 49.9, 46.6, 45.6, 45, 39.7, 37.7, 
30.7, 28.6, 15.9, 22.7, 37.7, 22, 59.6, 52, 47.9, 44.8, 43.5, 
43.4, 38, 36.5, 29.5, 27.4, 15, 21.6, 36.7, 21.4, 59.4, 52.5, 
48.4, 45.1, 44, 44.2, 38.5, 37.3, 30, 27.9, 15.3, 22.1, 36.8, 
22.5, 59.7, 53, 48.2, 44.8, 43.5, 43.7, 38.5, 37.1, 29.6, 27.8, 
15.1, 21.6, 37, 22, 60, 52.1, 47, 44.4, 42.7, 43.1, 37.9, 36.3, 
29.1, 27.2, 14.5, 20.9, 35.7, 21.4, 62.4, 55.1, 49, 46.4, 45, 
45.2, 40.2, 38.4, 30.7, 28.6, 15.2, 21.9, 37.7, 22.6, 61.8, 55.6, 
49.5, 46.4, 45.7, 45.6, 39.6, 38, 30.8, 28.6, 15.2, 22.3, 37.7, 
22.6, 62.7, 56.2, 49.9, 46.6, 45.7, 45.6, 39.7, 38.3, 30.6, 28.4, 
15.4, 22.2, 37.9, 23.4, 68.1, 60.8, 54.7, 50.5, 50.1, 49.1, 44.1, 
43, 33.9, 31.4, 17.5, 25, 40.2, 26.1, 66.4, 59.7, 53.7, 49.5, 
48.8, 47.7, 43.2, 41.7, 32.7, 30.2, 16.7, 23.8, 38.8, 25.4, 63.1, 
58.2, 52.5, 48.5, 47.5, 46.6, 41.5, 40.7, 31.7, 29.4, 16.1, 23, 
37.6, 23.8, 67.5, 61.2, 55.5, 51.2, 50.1, 49.5, 44.5, 43.8, 34, 
31.7, 18, 24.9, 40.9, 26.6, 68.5, 61.3, 56.3, 51.3, 50.6, 50, 
44.6, 44, 34.4, 31.8, 18.1, 25, 41, 26.6, 70.3, 62.2, 56.9, 52.5, 
51.3, 50.8, 45.5, 44.6, 34.8, 32.2, 18.6, 24.9, 40.7, 26.7, 69, 
60.7, 55.8, 51.5, 50, 50.7, 44.6, 44, 34.4, 31.6, 18.1, 24.3, 
40.2, 25.9, 65.9, 59.4, 53.9, 49.6, 48.5, 49.4, 42.7, 42.3, 33.3, 
30.2, 17.2, 23, 38.2, 24.2, 68.4, 61.9, 56.7, 52.5, 51.7, 51.8, 
45.1, 44.8, 35.4, 32.1, 18.9, 24.7, 41, 26.5, 69.7, 62.6, 56.8, 
52.6, 51.4, 51.9, 45.7, 44.7, 35.8, 32.2, 19, 25.2, 41.2, 26.5, 
67.8, 61.3, 55.7, 51.5, 50.8, 50.2, 44.6, 43.9, 35, 31.6, 17.9, 
24.4, 41.4, 26.8, 70.9, 63.4, 57.2, 53.5, 52.1, 51.3, 45.9, 45.1, 
36.5, 32.9, 19, 25.6, 42.9, 27.2, 70.4, 62.4, 56.9, 52.6, 51.4, 
50.5, 45.1, 44.8, 36.1, 32.6, 18.7, 25.3, 42.6, 27.1, 70.1, 63.2, 
57, 52.6, 51.1, 49.7, 45.1, 44.3, 36.2, 32.4, 18.5, 25.1, 41.6, 
27.1), date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 
22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 
23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
24L, 24L, 24L, 24L, 24L, 24L), .Label = c("2019_11_06", "2019_11_07", 
"2019_11_08", "2019_11_09", "2019_11_10", "2019_11_11", "2019_11_12", 
"2019_11_13", "2019_11_14", "2019_11_15", "2019_11_16", "2019_11_17", 
"2019_11_18", "2019_11_19", "2019_11_20", "2019_11_21", "2019_11_22", 
"2019_11_23", "2019_11_24", "2019_11_25", "2019_11_26", "2019_11_27", 
"2019_11_28", "2019_11_29"), class = "factor"), group = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L), .Label = c("a", 
"b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-336L))

ggplot(data=nat) + 
  geom_line(aes(x=date, y = value,colour=group))

This is the output :

enter image description here https://i.sstatic.net/OAIkW.png

I've looked at some of the recommended posts:

and can't find a solution from them.

I'm expecting to have multiple lines coloured by group.

For example (this is not representative of the style that I want, just a plot with a couple of lines coloured by group):

enter image description here

7
  • 1
    There's only one group a in your data, also there are multiple records per same date. What you could probably try is smooth - ggplot(nat) + geom_smooth(aes(date, value, colour = group, group = group)) Commented Dec 5, 2019 at 12:40
  • @PoGibas sorry, the data isn't properly representative then ( i chopped some ), I will update with a better data example. Commented Dec 5, 2019 at 12:51
  • there are multiple records for the same date because there are observations of different groups on the same date Commented Dec 5, 2019 at 12:58
  • @baxx there is only one group, a, and it has multiple rows for each date (14, look at table(nat$group, nat$date) with your posted data) , so I' sorry but I do not understand your comment. Commented Dec 5, 2019 at 13:01
  • 1
    thank you @s_t , i have updated the data again, there should be only one group for each date. Commented Dec 5, 2019 at 13:20

2 Answers 2

6

You can try something like this, I advice you to convert date as date, using for example lubridate::ymd():

library(tidyverse)
library(lubridate)
# your data
nat %>% 
# add date as date
  mutate(date = ymd(date)) %>%
# plot them
  ggplot( aes(x = date, y = value, color = group, group = group)) +
  geom_line() + geom_point() +   theme_test()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

I believe your biggest problem is that date is a factor and ggplot cannot really draw lines when the variable in the x-axis has this type.

If you convert it to date format (using the lubridate library), it works just fine:


library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

glimpse(nat)
#> Observations: 336
#> Variables: 4
#> $ X     <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18...
#> $ value <dbl> 56, 52, 48, 44, 43, 44, 37, 36, 29, 27, 15, 22, 36, 22, 59, 5...
#> $ date  <fct> 2019_11_06, 2019_11_06, 2019_11_06, 2019_11_06, 2019_11_06, 2...
#> $ group <fct> a, b, c, d, e, f, g, h, i, j, k, l, m, n, a, b, c, d, e, f, g...

nat %>%
    mutate(x = ymd(date)) %>%
    ggplot(aes(x = x, y = value, colour = group)) +
        geom_point() +
        geom_line()

Created on 2019-12-05 by the reprex package (v0.3.0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.