Home > Net >  Time Series Plot group by date in R
Time Series Plot group by date in R

Time:04-21

I have got the following data frame:

Tweets_text    date            Hashtag1     Hashtage2
xxxxxxxxxx     2021-01-01          1            0
xxxxxxxxxx     2021-01-01          1            1
xxxxxxxxxx     2021-01-02          0            1

And I want to create a plot that shows how many tweets were associated with each hashtag on each day. I've tried lots of methods but none of them work.

CodePudding user response:

If using ggplot2, then it is easiest to put it into long format first, and we can go ahead and summarize the data as well to make it easier to plot.

library(tidyverse)

df %>% 
  pivot_longer(-c(Tweets_text, date)) %>% 
  group_by(date, name) %>% 
  summarize(value = sum(value)) %>% 
  ggplot()  
  geom_line(aes(x = date, y = value, color = name, group = name))

Output

enter image description here

CodePudding user response:

library(tidyverse)

df %>% 
  pivot_longer(
    starts_with("Hash")
  ) %>% 
  group_by(date) %>% 
  summarise(sum = sum(value)) %>% 
  ggplot(aes(x = date, y=sum, group=1))  
  geom_point() 
  geom_line() 
  expand_limits(x = 0, y = 0) 
  theme_classic()

enter image description here

  •  Tags:  
  • r
  • Related