我正在嘗試按日期可視化情緒,日期格式為日、月、年。
這是我的資料集中的變數示例:
str(twitter_posts)
結果:
tibble [1,068 × 5] (S3: tbl_df/tbl/data.frame)
$ post : chr [1:1068] "?????? ????? ????? ???? ???????\r\n????? ???????? ?????? ??? ???? ???? ???? ??????? ???????\r\n?? ??????? ?? ??"| __truncated__ "????? ??? ????? ??????? ??? ?? ??? ??????\r\n\r\n?????? ?????\r\n\r\n???? ????? ???????? ???? ??????? ?????? ??"| __truncated__ "???? ???????? ??? ??????? ???? ?????\r\n????? ???????? ??????? ???\r\n?? ?? ????? ???? ??????" "???????? ??? ???? ???????" ...
$ date : chr [1:1068] "40643" "40643" "40673" "40673" ...
$ period : chr [1:1068] "10-Apr-11" "10-Apr-11" "10-May-11" "10-May-11" ...
$ sentiment : chr [1:1068] "neutral" "negative" "negative" "positive" ...
$ treatment_announcement: chr [1:1068] "pre" "pre" "pre" "pre" ...
我正在嘗試運行以下代碼,并使用下面推薦的代碼
twitter_posts %>%
mutate(date = as.Date(as.numeric(date), origin = "1899-12-30")) %>%
mutate(date = as.Date(period))%>%
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()
但我仍然收到時間變數錯誤:
“錯誤:mutate()列有問題date。?。xdate = as.Date(period)字串不是標準的明確格式運行rlang::last_error()以查看發生錯誤的位置。”
uj5u.com熱心網友回復:
嘗試這個:
twitter_posts %>%
mutate(date = as.Date(as.numeric(date), origin = "1899-12-30")) %>%
#updated
mutate(period = parse_date_time(period, "dmy") %>%
#mutate(date = as.Date(period))%>%
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/452273.html
