Search code examples
rdata-sciencertweet

time zone not working when plotting tweets with rtweet's ts_plot()


there is a closed issue on rtweet's GitHub saying that you could use tz in ts_plot https://github.com/ropensci/rtweet/issues/227

rt <- search_tweets("rstats", n = 500)

## with default timezone (UTC)
ts_plot(rt, "hours")

## with american central time zone
ts_plot(rt, "hours", tz = "US/Central") 

but i'm trying this in my code and I always get UTM hours

ts_plot(tweets, "mins", tz = "America/Montevideo") +
  labs(x = NULL, y = NULL,
       title = "Frequency of tweets",
       subtitle = paste0(format(min(tweets$created_at), "%d/%m/%Y - %H:%M:%S", tz = "America/Montevideo"), " to ", format(max(tweets$created_at),"%d/%m/%Y - %H:%M:%S", tz = "America/Montevideo")),
       caption = "Data collected from Twitter's REST API via rtweet") 

tz is working fine for the subtitle, but no for the actual t_plot, any ideas if this should be working or not?

I downloaded the package directly from GitHub with

## install dev version of rtweet from github
remotes::install_github("ropensci/rtweet")
library(rtweet)

Thanks


Solution

  • I see the same issue. I would format the time to your desired time zone in advanced with mutate() and the lubridate package.

    library(rtweet)
    library(dplyr)
    library(lubridate)
    
    rt <- search_tweets("rstats", n = 500) %>% 
      mutate(created_at = ymd_hms(format(created_at, tz = "America/Montevideo"))) 
    
    rt %>% 
      ts_plot("mins", tz = "America/Montevideo") +
      labs(x = NULL, y = NULL,
           title = "Frequency of tweets",
           subtitle = paste0(min(rt$created_at), " to ", max(rt$created_at)),
           caption = "Data collected from Twitter's REST API via rtweet") 
    

    Created on 2020-04-28 by the reprex package (v0.3.0)