My data tables can contain either daily data or weekly data from the year 2020. I create the scatter plot with the regression line as follows:
##########
## DAY: ##
##########
dt.day <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
DE = rnorm(365, 4, 1), Austria = rnorm(365, 10, 2),
Czechia = rnorm(365, 1, 2), check.names = FALSE)
## Linear regression: ##
regLine <- lm(DE ~ Austria, data = dt.day)
## PLOT: ##
p <- ggplot(data = dt.day, aes(x = Austria, y = DE,
text = paste("Date: ", date, '\n',
"Austria: ", Austria, "GWh/h", '\n',
"DE: ", DE, "\u20ac/MWh"),
group = 1)
) +
geom_point(color = "#419F44") +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
xlab("Austria") +
ylab("DE")
# Correlation plot converting from ggplot to plotly: #
AUSTRIA <- plotly::ggplotly(p, tooltip = "text")
###########
## WEEK: ##
###########
dt.week <- data.table(date = seq(as.Date('2020-01-01'), by = '7 day', length.out = 53),
Germany = rnorm(53, 4, 1), Austria = rnorm(53, 10, 2),
Czechia = rnorm(53, 1, 2), check.names = FALSE)
The plot for the daily data looks like this:
I would like to plot the last 10 days of my data table (regardless of how much data it contains, because it could only be data from January to April every day) in a different color ("#F07D00").
The plot for the weekly data table is plotted analogously. I would like to color differently the last 4 weeks.
I also have another question:
If I had a data table that had hourly entries every day (i.e. 24 per day), how would that work for the points for the last 2 days? The format of the 1st column (date) has the format "POSIXct" "POSIXt" as the following:

