Forums | OpenR
OpenR: R & Statistics
/
questions about exercise 1.4 of "R for time data"
Posts: 2
15 March 2023, 5:33 PM
dates <- seq(as.Date("2010-09-13"), as.Date("2040-09-13"), by="year")
weekdays <- format(dates, format = "%A")
weekdayplot <- data.frame(years=(format(dates, format = "%Y")),weekdays)
library(ggplot2)
ggplot(weekdayplot, aes(x = weekdays, y = years)) +
geom_point() +
labs(title = "September 13th of 2000 to 2050", x = "Weekday", y = "Year")
plot(years ~ as.factor(weekdays), weekdayplot, main = title)
as the codes shows above, why there must be "as.factor" in the last line of codes or will encounter errors like this:
additionally, the x axisof the figure obtained by the codes of last line are numbers instead of "Monday"....
is that true that the xaxis must be numbers?
Posts: 128
17 March 2023, 8:50 AM
These two questions are both related to factors.
If "weekdays" is not converted into a factor, the plot() function cannot get the x coordinate and fails in mapping the values on the graph.
A character object is nominal, while a factor is ordinal. The sequence of the members in a factor is the x coordinates for plotting.
More details could be found here.
3 results