[英]how to plot timeDate using ggplot2 in R?
library(timeDate)
library(ggplot2)
library(ggrepel)
dataset1$TimeStamp <- timeDate(dataset1$TimeStamp, format = "%Y/%m/%d %H:%M:%S", zone = "GMT", FinCenter = "GMT")
p1 <- ggplot(dataset1, aes(x = TimeStamp, y = y1))
p1 +
geom_point() +
geom_text_repel(aes(label = Label1), size = 3)
注意:執行上面的代碼時,我會看到下一個:不知道如何自動為timeDate類型的對象選擇比例。 默認為連續。 錯誤:geom_point需要以下美感:x
如何在ggplot中使用timeDate類?
ggplot可能不知道如何處理timeDate
類。 您只需將TimeStamp
的@Data
插槽中的值插入ggplot:
dataset1 <- data.frame(TimeStamp = sample(1:100,50,replace = T),
y1=sample(1:50,50,replace=T),
Label1 = sample(LETTERS[1:5],50,replace=T)
)
dataset1$TimeStamp <- timeDate(dataset1$TimeStamp,
format = "%Y/%m/%d %H:%M:%S",
zone = "GMT",
FinCenter = "GMT"
)
str(dataset1$TimeStamp)
# Formal class 'timeDate' [package "timeDate"] with 3 slots
# ..@ Data : POSIXct[1:100], format: "1970-01-01 00:00:09" "1970-01-01 00:00:05" "1970-01-01 00:00:06" ...
# ..@ format : chr "%Y-%m-%d %H:%M:%S"
# ..@ FinCenter: chr "GMT"
str(dataset1$TimeStamp@Data)
# Dates in POSIXct format are storred in @Data slot
# POSIXct[1:100], format: "1970-01-01 00:00:09" "1970-01-01 00:00:05" "1970-01-01 00:00:06" "1970-01-01 00:00:04" ...
ggplot(dataset1, aes(x = TimeStamp@Data, y = y1, colour = Label1)) +
geom_point() +
geom_text_repel(aes(label = Label1, colour = Label1), size = 3) +
theme_dark() +
labs(x="Time Stamp", y = "Value") +
scale_colour_discrete(guide = F)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.