Reputation: 19
The timestamp data is in chr as follows:
posted_at: chr "2012-01-29 19:48:33" "2012-02-02 15:53:13" "2012-10-24 17:11:40" "2014-07-12 17:00:00" ...
[1] "2012-01-29 19:48:33" "2012-02-02 15:53:13" "2012-10-24 17:11:40" "2014-07-12 17:00:00" "2014-07-31 08:08:31"
[6] "2014-07-31 10:48:25" "2014-08-06 09:24:38" "2015-06-16 15:55:28" "2015-06-16 19:56:28" "2015-06-25 17:20:29"
[11] "2015-06-26 18:28:31"
I tried converting it using strptime() :
tweet_text$posted_at <- strptime(trump_text$posted_at, "%Y-%m-%d %H:%M:%S")
It gets converted as follows:
posted_at: POSIXlt, format: "2012-01-29 19:48:33" "2012-02-02 15:53:13" "2012-10-24 17:11:40" "2014-07-12 17:00:00" ...
[1] "2012-01-29 19:48:33 AEDT" "2012-02-02 15:53:13 AEDT" "2012-10-24 17:11:40 AEDT" "2014-07-12 17:00:00 AEST"
[5] "2014-07-31 08:08:31 AEST" "2014-07-31 10:48:25 AEST" "2014-08-06 09:24:38 AEST" "2015-06-16 15:55:28 AEST"
[9] "2015-06-16 19:56:28 AEST" "2015-06-25 17:20:29 AEST" "2015-06-26 18:28:31 AEST" "2015-07-01 15:58:41 AEST"
[13] "2015-07-01 18:05:15 AEST
How do I use hist() to plot this timestamp data based on years and count and other different combinations .
Upvotes: 0
Views: 316
Reputation: 389055
POSIXlt
class already has year component in it which shows number of years since 1900. You can add 1900 to the year and use it in hist
.
hist(trump_text$posted_at$year + 1900)
Using ggplot2
you can do :
library(dplyr)
library(ggplot2)
trump_text %>%
mutate(year = format(posted_at, '%Y')) %>%
ggplot() + aes(year) + geom_histogram(stat = 'count')
You can customize/update the plot based on your choice.
Upvotes: 1
Reputation: 887251
We can take the frequency count with table
on the extracted 'Year' from the Datetime object and do a barplot
or hist
. No external packages used
hist(as.numeric(format(trump_text$posted_at, '%Y')))
Using a reproducible exampe
v1 <- as.POSIXlt(sample(seq(Sys.time(), length.out = 20, by = 'year'), 200, replace = TRUE))
hist(as.numeric(format(v1, '%Y')))
Or another option is table
with barplot
barplot(table(format(v1, '%Y')))
Or using tidyverse
library(dplyr)
library(lubridate)
library(ggplot2)
tibble(v1 = v1) %>%
mutate(year = year(v1)) %>%
ggplot(aes(year)) +
geom_histogram(stat = 'count')
-output
Upvotes: 1