Home > Enterprise >  Visualizing sentiment over time with ggplot2
Visualizing sentiment over time with ggplot2

Time:11-24

I am trying to visualize sentiment over time similar to the post enter image description here

Thanks to the advice below, I ran the following and it works well:

Visualizing_sentiment %>%
  mutate(date = as.Date(date))%>%
  count(sentiment, date)%>%
  ggplot(aes(x = date, y = n, fill = sentiment)) 
  geom_col()  
  #geom_col(position = "dodge") 
  scale_fill_manual(values = c("positive" = "green", 
                               "negative" = "red", 
                               "neutral"= "black")) 
    scale_x_date(date_labels = "%b-%y") 
    facet_wrap(~ year(date))
  theme_classic()

CodePudding user response:

To plot sentiment over time you need a date column and a sentiment column. Then you can count the sentiment by date with count(sentiment, date) and then you can plot date along the x axis, n up the y axis, and fill by sentiment.

If you want stacked bar charts, remove position = "dodge" from geom_col()


library(lubridate)
library(tidyverse)

data <- tibble(
  sentiment = c("positive", "positive", "negative", "negative", "neutral", "neutral",
                "neutral", "positive", "negative", "neutral", "neutral", "negative",
                "negative", "neutral", "neutral", "positive"),
  date = c("2010-02-03", "2010-02-03", "2010-02-04", "2010-02-04", "2010-02-04", "2010-02-05",
           "2010-02-05", "2010-02-05", "2010-02-05", "2010-02-05", "2010-02-03", "2010-02-04",
           "2010-02-04", "2010-02-05", "2010-02-04", "2010-02-04")
)
data %>%
  mutate(date = as.Date(date))%>%
  count(sentiment, date)%>%
  ggplot(aes(x = date, y = n, fill = sentiment)) 
  geom_col(position = "dodge") 
  scale_fill_manual(values = c("positive" = "green", 
                               "negative" = "red", 
                               "neutral"= "black")) 
  scale_x_date(date_labels = "%b-%Y") 
  theme_bw()

I added a scale_x_date() for you. the %b represents Month abbreviation, and the %Y is the year. If you just wanted say '10' instead of '2010' you could go for %y.

A recommendation if doing this for multiple years, would be an extra step, and use a facet_wrap() to show each year as a separate chart. Which you could do like so:

previous_plotting_code 
    facet_wrap(~ year(date))

the year() function will pick out the year from the date variable, courtesy of lubridate.

  • Related