Вы можете напрямую применить as.Date()
, например, так:
mich_polls$enddate <- as.Date(mich_polls$enddate, format="%m/%d/%y")
В графике используйте scale_x_date()
и параметр date_breaks
, где вы можете применять значения, такие как "2 weeks"
, "1 month"
и подобныевещей;просто посмотрите в ?scale_x_date
, после загрузки library(ggplot2)
конечно.
library(ggplot2)
ggplot(data = mich_polls, aes(x = enddate, y = rawpoll_clinton, group=2)) +
geom_line(aes(colour = pollster, size = 1.5)) +
scale_x_date(date_breaks="1 month") +
theme(axis.text.x = element_text(angle = 40, hjust = 1))
Выход:
Данные:
mich_pols <- structure(list(pollster = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L), .Label = c("EPIC-MRA", "Glengariff Group, Inc."
), class = "factor"), enddate = structure(c(18287, 18343, 18478,
18518, 18538, 18559, 18569, 18408, 18475, 18533, 18546), class = "Date"),
rawpoll_clinton = c(51.2, 56, 57.3, 52.1, 57.3, 54.7, 52.5,
52.9, 56.5, 54.5, 58), rawpoll_trump = c(48.8, 44, 42.7,
47.9, 42.7, 45.3, 47.5, 47.1, 43.5, 45.5, 42)), row.names = c("4208",
"4168", "4162", "1187", "747", "368", "124", "3132", "2282",
"789", "436"), class = "data.frame")