Visualize Twitter sentiment over time using a smoothed line plot
STEP 1: Load the dataset
data <-read.csv("C:/Users/Admin/OneDrive/Desktop/test.csv")head(data)
id
1 31963
2 31964
3 31965
4 31966
5 31967
6 31968
tweet
1 #studiolife #aislife #requires #passion #dedication #willpower to find #newmaterialsâ\u0080¦
2 @user #white #supremacists want everyone to see the new â\u0080\u0098 #birdsâ\u0080\u0099 #movie â\u0080\u0094 and hereâ\u0080\u0099s why
3 safe ways to heal your #acne!! #altwaystoheal #healthy #healing!!
4 is the hp and the cursed child book up for reservations already? if yes, where? if no, when? ð\u009f\u0098\u008dð\u009f\u0098\u008dð\u009f\u0098\u008d #harrypotter #pottermore #favorite
5 3rd #bihday to my amazing, hilarious #nephew eli ahmir! uncle dave loves you and missesâ\u0080¦
6 choose to be :) #momtips
strsplit() splits tweet into words
%in% checks if word is in positive/negative list
score = positive count - negative count
Step 5: Use id as time
Code:
data$time <- data$id
Explanation:
Since no date column exists, id is used as time order.
Step 6: Load ggplot2
Code:
library(ggplot2)
Explanation: Used for creating the graph.
Step 7: Create smoothed line plot
Code:
ggplot(data, aes(x = time, y = score)) +geom_smooth(color ="blue", fill ="lightblue") +labs( title ="Twitter Sentiment Over Time",x ="Tweet Order",y ="Sentiment Score" ) +theme_minimal()
`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
ggplot(data, aes(x = time, y = score, color ="Sentiment Trend")) +geom_smooth(fill ="lightblue", se =TRUE, method ="loess") +labs( title ="Twitter Sentiment Over Time",x ="Tweet Order", y ="Sentiment Score",color ="Legend" ) +theme_minimal() +theme(legend.position ="top")
`geom_smooth()` using formula = 'y ~ x'
Explanation:
Creates legend for better presentation.
Interpretation :
The graph shows how sentiment changes across tweets. Since the dataset does not include time information, tweet order is used as a representation of time. Positive scores indicate positive sentiment and negative scores indicate negative sentiment. The smoothed curve helps in identifying overall trends by reducing short-term variations.