I would like to ask for help. I'm analyzing a univariate time series on Rstudio. I would like to reproduce the plot of the series and modify the values present on the x axis. the time series is saved in a txt file. this is my code:
dati <- read.table("positivi.txt", header=TRUE)
plot.ts(dati, main="positivi giornaliei",col=4)
On the x axis I would like to insert the dates, how can I do?
The data file consists of 123 observations of integer values
dput(head(newdati,123))
c(245L, 127L, 252L, 243L, 300L, 271L, 223L, 135L, 82L, 244L,
212L, 201L, 175L, 229L, 109L, 41L, 235L, 215L, 125L, 113L, 131L,
107L, 37L, 143L, 156L, 126L, 110L, 124L, 71L, 27L, 146L, 124L,
96L, 95L, 116L, 116L, 32L, 190L, 103L, 108L, 71L, 69L, 64L, 59L,
120L, 122L, 160L, 138L, 122L, 148L, 45L, 154L, 163L, 201L, 172L,
159L, 131L, 44L, 63L, 225L, 200L, 173L, 228L, 159L, 62L, 184L,
152L, 218L, 153L, 183L, 112L, 70L, 199L, 262L, 158L, 156L, 208L,
151L, 109L, 217L, 244L, 224L, 250L, 331L, 158L, 127L, 319L, 381L,
244L, 347L, 374L, 253L, 155L, 361L, 571L, 146L, 417L, 621L, 284L,
209L, 547L, 474L, 497L, 399L, 705L, 463L, 188L, 501L, 684L, 688L,
719L, 601L, 161L, 366L, 1169L, 1060L, 1402L, 738L, 355L, 136L,
211L, 480L, 504L)
CodePudding user response:
If you want a ggplot2 graph, create a dates variable with seq.Date and then plot the data in a R 4.1.0 pipe.
library(ggplot2)
start <- as.Date("2021-09-04") dates <- seq(start, by = "1 day", length.out = length(dati))
data.frame(dati, dates) |>
ggplot(aes(dates, dati))
geom_line()
scale_x_date(date_breaks = "week", date_labels = "%d %b %Y")
theme_bw()
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
CodePudding user response:
You more than likely need to use the axis() command to do this.
It will be something along the lines of
plot.ts(dati, main="positivi giornaliei",col=4, xaxt = 'n')
axis(1, at = 1:length(dati), labels = yourLabels)
where yourLabels is a vector with all your dates in.
xaxt = 'n' stops the x-axis from being plotted, and then the axis() command adds a new one in

