0

So I have ran into a problem where I can only get my data points to align with the x-axis labels when I specify in scale_x_date(..., date_breaks = "days") which misrepresents the data. Could someone please give some indication why they aren't aligned and how to fix it?

tidy_sales$Week_End <- as.Date(tidy_sales$Week_End, origin = 2022-02-06)

g1 <- ggplot(tidy_sales, aes(Week_End, y = `Value($)`)) +
  geom_line(aes(color = variable)) +
  geom_point(aes(col = variable))



g1 +  scale_x_date(date_labels="%b-%d", date_breaks  = "week",
                   limits = c(min(tidy_sales$Week_End),
                              max = max(tidy_sales$Week_End))) +
      theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))



dput(tidy_sales)
structure(list(Week_End = structure(c(19043, 19036, 19029, 19050, 
19043, 19036, 19029, 19050, 19043, 19036, 19029, 19050), class = "Date"), 
    variable = c("Total_Expenses ($)", "Total_Expenses ($)", 
    "Total_Expenses ($)", "Total_Expenses ($)", "Total_Sales($)", 
    "Total_Sales($)", "Total_Sales($)", "Total_Sales($)", "Actual_Revenue ($)", 
    "Actual_Revenue ($)", "Actual_Revenue ($)", "Actual_Revenue ($)"
    ), `Value($)` = c(6020.94, 7233.68, 5153.67, 5086, 13847.58, 
    9928.14, 15053.49, 14111.2, 12588.71, 9025.58, 13684.99, 
    12828.36)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

benson23
  • 16,369
  • 9
  • 19
  • 38
  • 6
    Instead of `date_breaks`, try `breaks = unique(tidy_sales$Week_End)` – Allan Cameron Mar 03 '22 at 00:16
  • Please provide your data or mock data that resembles your data, in order to make make your question a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). You could do that by including the output of `dput(tidy_sales)` in your question. – Till Mar 03 '22 at 00:18

0 Answers0