我對每小時可視化給定主題的 Twitter 情緒感興趣,我的變數存盤如下:
sapply(valence_hour,class)
Time day mean_valence n
"character" "numeric" "numeric" "integer"
這是一個資料示例:
Time day mean_valence n
23:59:00 19 0.0909090909 3
23:58:00 19 0.0589743590 3
23:57:00 19 0.49743590 3
然后,我為圖表運行了以下代碼:
ggplot(valence_hour, aes(x = Time, y = mean_valence))
geom_point()
geom_line()
scale_x_continuous(breaks=seq(1,30,1))
geom_smooth()
但是,我不斷收到此錯誤:“錯誤:提供給連續刻度的離散值”
為了解決這個我認為是由存盤為字符的“時間”變數引起的問題,我嘗試實施類似于
uj5u.com熱心網友回復:
這是一種方法。
連接當前系統日期和Time,強制"POSIXct"使用這個新的臨時變數作為 x 軸。在日期時間圖層中設定軸標簽。
警告是由于資料集小,loess抱怨沒有足夠的資料點。不用擔心,它將適用于更大的資料。
library(dplyr, quietly = TRUE)
library(ggplot2, quietly = TRUE)
x <- '
Time day mean_valence n
23:59:00 19 0.0909090909 3
23:58:00 19 0.0589743590 3
23:57:00 19 0.49743590 3'
valence_hour <- read.table(textConnection(x), header = TRUE)
valence_hour %>%
mutate(Time = paste(Sys.Date(), Time),
Time = as.POSIXct(Time)) %>%
ggplot(aes(Time, mean_valence))
geom_point()
geom_line()
scale_x_datetime(
date_breaks = "1 mins",
date_labels = "%H:%M:%S"
)
geom_smooth(formula = y ~ x, method = "loess")
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : span too small. fewer data values than degrees of freedom.
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : pseudoinverse used at 1.654e 09
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : neighborhood radius 60.6
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : reciprocal condition number 0
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : There are other near singularities as well. 3672.4
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : span too small. fewer
#> data values than degrees of freedom.
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
#> 1.654e 09
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius 60.6
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
#> number 0
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : There are other near
#> singularities as well. 3672.4
#> Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
#> -Inf

由reprex 包于 2022-05-30 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/483763.html
