Espejito
Espejito

Reputation: 480

R: two line graphs from date objects in one plot

In the my data frame I have two columns, they are both date objects (Date1, Date2). I want to draw a line of both of the date objects and place them into the same plot, but can't find a way. Should I use pivot_longer?

The plot with one variable

df %>%
  mutate(week = week(Date1)) %>%
  ggplot(aes(week)) +
  geom_freqpoly()

Data

structure(list(Date1 = structure(c(1583712000, 1583712000, 1583712000, 
1584144000, 1584316800, 1584489600, 1585094400, 1585267200, 1585267200, 
1588550400, 1589414400, 1591228800, 1592179200, 1597104000, 1597363200, 
1597881600, 1605052800, 1605484800, 1614902400, 1580256000, 1582502400, 
1582761600, 1582848000, 1583107200, 1583107200, 1583712000, 1583712000, 
1583884800, 1583884800, 1584316800, 1584316800, 1584316800, 1584316800, 
1584316800, 1584316800, 1584316800, 1584489600, 1584921600, 1585180800, 
1585267200, 1587513600, 1591228800, 1602460800, 1604016000, 1613779200, 
1613779200, 1614038400, 1615852800, 1615852800, 1615852800, 1623024000, 
1624233600, 1629244800, 1630281600, 1582588800, 1582848000, 1582848000, 
1583193600, 1583193600, 1583193600, 1583280000, 1583712000, 1583712000, 
1583712000, 1583884800, 1583884800, 1583971200, 1583884800, 1615593600, 
1615939200, 1584489600, 1584662400, 1584662400, 1584662400, 1584921600, 
1585094400, 1585180800, 1585180800, 1585526400, 1585785600, 1586304000, 
1587600000, 1586131200, 1586131200, 1586217600, 1586908800, 1587340800, 
1587340800, 1587340800, 1587340800, 1587513600, 1587600000, 1587945600, 
1588032000, 1588723200, 1589155200, 1589241600, 1589414400, 1589760000, 
1589760000, 1589932800, 1590969600, 1596240000, 1596240000, 1598832000, 
1601510400, 1601596800, 1602720000, 1602547200, 1602547200, 1602547200, 
1603152000, 1604620800, 1604620800, 1605225600, 1605225600, 1606435200, 
1606435200, 1607644800, 1607644800, 1578355200, 1610323200, 1607904000, 
1610582400, 1611532800, 1580169600, 1613692800, 1613692800, 1583280000, 
1583366400, 1615334400, 1616716800, 1618358400, 1618790400, 1619049600, 
1619568000, 1619395200, 1622073600, 1623024000, 1612915200, 1620777600
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Date2 = structure(c(1579824000, 
1579824000, 1581552000, 1582243200, 1583193600, 1583193600, 1583366400, 
1583366400, 1583366400, 1583452800, 1583971200, 1585526400, 1583971200, 
1583971200, 1584057600, 1584057600, 1584316800, 1584316800, 1584489600, 
1584576000, 1584576000, 1584662400, 1585267200, 1585267200, 1585526400, 
1585785600, 1585785600, 1585872000, 1586390400, 1586908800, 1587600000, 
1587945600, 1588032000, 1588032000, 1588636800, 1588809600, 1588809600, 
1589241600, 1589414400, 1591660800, 1592438400, 1596412800, 1596499200, 
1597190400, 1598486400, 1598572800, 1598572800, 1600300800, 1603411200, 
1604275200, 1606348800, 1606521600, 1606780800, 1607472000, 1611273600, 
1611705600, 1612224000, 1614211200, 1614556800, 1616025600, 1616025600, 
1618444800, 1618444800, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 
-141L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 3

Views: 74

Answers (4)

TarJae
TarJae

Reputation: 78927

Here is a version with geom_line:

library(tidyverse)
df %>% 
    mutate(across(everything(), ~week(.))) %>% 
    pivot_longer(everything(), 
                 names_to = "name",
                 values_to = "week") %>% 
    count(name, week) %>% 
    ggplot(aes(x= week, y=n, color=name, group=name)) +
    geom_line() +
    coord_cartesian(ylim = c(0, 20))

enter image description here

Upvotes: 2

Anoushiravan R
Anoushiravan R

Reputation: 21908

We first extract the number of complete 7-day period with lubridate::week as you specified, then we use tidyr::pivotlonger to store the names of two newly created variables in one variable so that we could use it to map it to colour aesthetic in order to differentiate between the lines:

library(tidyr)
library(lubridate)
library(ggplot2)

df %>%
  mutate(across(everything(), ~ week(.x), .names = "{.col}_Week")) %>% 
  pivot_longer(c(Date1_Week, Date2_Week), names_to = "Date", values_to = "Value") %>%
  ggplot(aes(Value, colour = Date)) +
  geom_freqpoly(size = 1, binwidth = 0.9) + 
  labs(x = "Week")

enter image description here

Upvotes: 2

Peter
Peter

Reputation: 12699

And another variation on the theme:


library(ggplot2)
library(dplyr)
library(tidyr)
library(lubridate)

df %>%
  pivot_longer(everything())%>% 
  mutate(week = week(value)) %>%
  ggplot(aes(week, colour = name)) +
  geom_freqpoly()

Created on 2021-09-01 by the reprex package (v2.0.0)

Upvotes: 1

Vinícius Félix
Vinícius Félix

Reputation: 8811

Here is a simple solution, using colors to differentiate the dates

library(lubridate)
library(tidyverse)

df %>% 
  ggplot()+
  geom_freqpoly(aes(week(Date1), col = "Date1"))+
  geom_freqpoly(aes(week(Date2), col = "Date2"))

enter image description here

Upvotes: 1

Related Questions