-1

I am relatively new to GGPLOT2 but have an issue I don't quite understand. I have plot I am creating using GEOM_POINT with GEOM_LINE. Everything is looking great except the dates are not plotting in order as I would expect. I have 10 dates - 4/6/2018, 4/11/2018, 4/20/2018, 4/25/2018, 5/11/2018, 5/16/2018, 5/25/2018, 5/30/2018, 6/8/2018 6/13/2018. My plot returns these with for example 4/6/2018 AFTER 4/25/2018 even though I used ARRANGE with DPLYR. Any help for a bit of a newbie? Code is:

library(ggplot2)
library(dplyr)
library(plotly)
library(sqldf)
library(tidyverse)
library(lubridate)
library(rio) #lets you use "import" for any file - without using extension name
options(scipen =999) #disable scientific notation
windowsFonts(adi=windowsFont("TT adineue PRO Black")) #define adineue PRO Black font to use later

#prepare data:

setwd("C:/Users/hayescod/Desktop/BuysToForecastTracking")
Buys_To_Forecast <- import("BuysToForecastTrack.csv")
Buys_To_Forecast[is.na(Buys_To_Forecast)] <- " " #replace all NA with blanks

colnames(Buys_To_Forecast) <- c("Date", "BusinessSegment", "Material", "StockNumber", "POCreatedBy", "PlantCode", "StockCategory", "Description", "Excess", "QuantityBought", "WareHouseSalesOrders", "GrandTotal", "Comments" )
Buys_To_Forecast$PlantCode <-factor(Buys_To_Forecast$PlantCode) #update PlantCode to factor
Buys_To_Forecast$QuantityBought <- as.numeric(Buys_To_Forecast$QuantityBought)
Buys_To_Forecast$Date <- as.Date(Buys_To_Forecast$Date, format = %m%d%Y)


#use DPLYR to filter data: 

btf <- Buys_To_Forecast %>%
  group_by(Date, Comments) %>%
  summarize(QuantityBought = sum(QuantityBought)) %>%
  arrange(Date) %>%
  ungroup()

#use ggplot:

btfnew <- ggplot(data=btf, aes(x=Date, y=QuantityBought, group=1, color=Comments)) + #note to use group when COMBINING geom_point and geom_line
  geom_point(size=8) + 
  geom_line(size=3) +
  facet_grid(Comments~.,scales="free_y")+ #make the Y axis "free"
  xlab("Date") + 
  ylab("Quantity") +
  ggtitle("Buys To Forecast Trend")+
  theme_dark() +
  theme(plot.title = element_text(hjust = 0.5, size=30,color="Black",family="adi"), 
        axis.title.x = element_text(color="DarkGray", size = 24,family="adi"), 
        axis.title.y = element_text(color="DarkGray", size = 24,family="adi"),
        axis.text.x = element_text(size=15),
        axis.text.y = element_text(size=15),
        legend.title = element_text(size=24, family="adi"),
        legend.text = element_text(size=12),
        strip.text = element_text(size=15))

#display the plot in ggplot and add limits to the axis: 

btfnew + 
  expand_limits(y = 0) #way to force set axis' to 0 - in this case the Y axis to be uniform across facets
user3496218
  • 185
  • 3
  • 5
  • 19
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Remove any code not directly related to your specific problem. But most likely your "date" values are not proper date values in R and are probably strings or factors. Try parsing them with a `strftime()` or [lubridate](https://stackoverflow.com/questions/35807501/parse-datetime-with-lubridate). – MrFlick Jun 21 '18 at 18:30
  • Thanks for the input - i will try to provide a reproducible example. – user3496218 Jun 21 '18 at 21:35
  • I was able to use the the function the issue seems to be it still returns the type of character and the dates are out of order in the plot - the example 4/20/18 coming before 4/6/18 - I am assuming it needs to be in date format for GGplot to get it right? – user3496218 Jun 21 '18 at 22:47
  • Hi all - thank you for the replies. I am posting this as an answer although I don't fully feel like it is. I went into the source data and changed the date format to always read "mm/dd/yyyy" in Excel. After reloading the data, this worked in keeping things in order. My issue is I don't feel that is fully proper way to solve the issue - the data is still being read as a character and I have yet to find a way to get it read as a date in that format without either getting the dates in the wrong order OR getting a range of dates that don't represent the actual dates – user3496218 Jun 22 '18 at 03:05

1 Answers1

0

You should include a reproducible example so we can know exactly what is happening with your data. But the problem is probably with the parsing.

You could try parsing your date variable with mdy() function from lubridate package.

Hope it helps :)

Giovana Stein
  • 451
  • 3
  • 13