'Time Series Plot group by date in R
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.
Solution 1:[1]
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
Solution 2:[2]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AndrewGB |
| Solution 2 | TarJae |


