Date on X axis is not in date format in R visualization

143 views Asked by At

I am having a problem with Date in R, in my dataset the format of my date is like this: 15.01.2018, but in my X, I see [0, 50, 100, ...] instead of date.

enter image description here

This is my simple program to have a line chart that X axis should be my date (that is 365 days), could you please let me know what is my mistake?

library(ggplot2)
v <- MyDataSet$ACTUAL/100000
t <- MyDataSet$FORECAST/100000
mese <- as.Date(MyDataSet$CalDay , format = "%m.%d.%Y", frequency = 6)
plot(v,type = "l",col = "red", xlab = mese, ylab = "Actual in Milion", 
   main = "Actual Forecst")
lines(t, type = "l", col = "blue")

I was expecting to see all days, or at least group them by month... But do not know how I can do it.

P.S. This is my dataset, it's an Excel file with rows like below, I upload it as a DataModel in SAP Analytics and read it.

enter image description here

Many thanks

1

There are 1 answers

1
deepseefan On

I am not sure what kind of output you expect. The plot below is forecast by date and the color is the actual.

library(ggplot2)
library(lubridate)
library(scales)

# convert Calday to proper date formate using lubridate
df$Calday <- dmy(df$Calday)

base <- ggplot(df, aes(x=Calday, y = FORECAST/100000, color=Actual/100000))
base + geom_line() +  scale_x_date(labels = date_format("%d-%m"))

Output

sample_out

Data

df <- data.frame(Calday = c("08.01.2018", "15.01.2018", "22.01.2018", "02.01.2018", "09.01.2018", "16.01.2018"), 
                 Actual = c(2262, 2490, 1930, 9898, 860, 440), FORECAST = c(72485, 69961, 36368, 22618, 18703,22568 ))