I'm working on a Shiny app, where one of the options is aggregating data by Year-Month-Day of the week.
library(ggplot2)
library(dplyr)
pnd <- data.frame(c(rep('MEXICALI',900),rep('SALTILLO',900) ),sample(200:1600, 1800, T),sample(200:1600, 1800, T),rep(seq.POSIXt(from = as.POSIXct(Sys.Date()-90), length.out = 900, by = "1 hour"),2))
colnames(pnd) <- c('zona_carga', 'PrecioMDA', 'PrecioMTR', 'ID')
pnd <- pnd %>% select(ID, zona_carga,PrecioMDA, PrecioMTR) %>%
mutate(ID = format(ID, '%Y-%m %a') ) %>% group_by( ID, zona_carga) %>% summarise(PrecioMDA = mean(PrecioMDA), PrecioMTR = mean(PrecioMTR))
colors <- c('MEXICALI - PrecioMDA' = 'steelblue', 'SALTILLO - PrecioMTR' = 'magenta')
ggplot(pnd, aes(x = ID) ) +
geom_line(data = filter(pnd, zona_carga == 'MEXICALI'), aes(y = as.numeric(PrecioMDA),group='PrecioMDA', color = paste('MEXICALI','-','PrecioMDA')) ) +
geom_line(data = filter(pnd, zona_carga == 'SALTILLO'), aes(y = as.numeric(PrecioMTR),group='PrecioMTR', color = paste('SALTILLO','-','PrecioMTR') )) +
labs(y='$MXN/MWh',x='Fecha',color = 'legend') + scale_color_manual(values = colors) +
scale_x_discrete( )
The problem is that, when the date interval increase, labels start to get mixed. Is there any way to specify dynamic breaks in my x axis? something similar to scale_x_date(breaks = '1 day')