rdateggplot2linegraphformatdatetime

Date format Ggplot2 linegraph R


I want to output date in 2018-03-28 format. When I am trying to do that, I get only dots in the line graph: enter image description here

When using as.Date or strptime (it doesn't matter), It gives only: jan, feb, mar, apr. But the lines are working: enter image description here

This is my code for get the lines working with jan, feb, mar.

  output$shotAnalyse2 <- renderPlot({


  head(rsShotResult)


  if(input$shotAnalyse2ShotType == "free_throw"){
    position <- 0
    updateSelectInput(session, "shotAnalyse2Position", selected = 0)
  } else{
    position <- input$shotAnalyse2Position
  }
  ggplot(rsShotResult[rsShotResult$fullname %in% input$shotAnalyse2Players
                      &
                        as.Date(rsShotResult$startdate) <= input$shotAnalyse2Date[2]
                      &
                        as.Date(rsShotResult$startdate) >= input$shotAnalyse2Date[1]
                      &
                        rsShotResult$value3 == position
                      & 
                        rsShotResult$value4 == input$shotAnalyse2ShotType
                      , ],
         aes(x = strptime(starttime, format="%Y-%m-%d"),
             y = percentage)) +
    geom_line(aes(colour = as.character(accountid))) +
     geom_point(aes(colour = as.character(accountid))) +
    xlab("starttime") +
    scale_colour_manual(
      values = palette("default"),
      name = "Players",
      breaks = rsShotResult$accountid
    )

When running the command: dput(head(rsShotResult)) I get this result:

structure(list(accountid = c(22, 22, 27, 28, 28, 30), firstname = c("Henk", 
"Henk", "Tim", "Dean", "Dean", "Max"), lastname = c("Wilders", 
"Wilders", "Sneijder", "Babel", "Babel", "Pele"), starttime = c("2017-12-13", 
"2018-03-09", "2017-12-13", "2017-12-13", "2018-03-09", "2017-12-13"
), value = c(16, 7, 41, 7, 19, 16), value2 = c(23, 10, 47, 16, 
20, 20), value3 = c("0", "0", "0", "0", "0", "0"), value4 = c("free_throw", 
"free_throw", "free_throw", "free_throw", "free_throw", "free_throw"
), startdate = c("2017-12-13", "2018-03-09", "2017-12-13", "2017-12-13", 
"2018-03-09", "2017-12-13"), fullname = c("Henk Wilders", "Henk Wilders", 
"Tim Sneijder", "Dean Babel", "Dean Babel", "Max Pele"
), percentage = c(69.5652173913043, 70, 87.2340425531915, 43.75, 
95, 80), points = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_)), .Names = c("accountid", "firstname", "lastname", 
"starttime", "value", "value2", "value3", "value4", "startdate", 
"fullname", "percentage", "points"), row.names = c(NA, 6L), class = "data.frame")

Solution

  • You might try this

    #convert to class date
    rsShotResult$starttime <- as.Date(rsShotResult$starttime)
    
    ggplot(rsShotResult, aes(starttime, percentage, col = as.factor(accountid))) +
     geom_point() +
     geom_line() +
     scale_x_date(date_labels = "%Y-%m-%d",
                  breaks = df$starttime)
    

    enter image description here


    The problem with your first plot is that rsShotResult$starttime is of class character. While plotting, you should have read the following

    geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

    Hence you could also have used this code:

    ggplot(rsShotResult, aes(starttime, percentage, col = as.factor(accountid))) +
     geom_point() +
     geom_line(aes(group = accountid))
    

    enter image description here

    However, this results in a discrete scale and I'd not recommend to plot your data this way since date is continuous. But that is up to you of course.

    I hope this helps.

    data

    rsShotResult <- structure(list(accountid = c(22, 22, 27, 28, 28, 30), firstname = c("Henk", 
    "Henk", "Tim", "Dean", "Dean", "Max"), lastname = c("Wilders", 
    "Wilders", "Sneijder", "Babel", "Babel", "Pele"), starttime = c("2017-12-13", 
    "2018-03-09", "2017-12-13", "2017-12-13", "2018-03-09", "2017-12-13"
    ), value = c(16, 7, 41, 7, 19, 16), value2 = c(23, 10, 47, 16, 
    20, 20), value3 = c("0", "0", "0", "0", "0", "0"), value4 = c("free_throw", 
    "free_throw", "free_throw", "free_throw", "free_throw", "free_throw"
    ), startdate = c("2017-12-13", "2018-03-09", "2017-12-13", "2017-12-13", 
    "2018-03-09", "2017-12-13"), fullname = c("Henk Wilders", "Henk Wilders", 
    "Tim Sneijder", "Dean Babel", "Dean Babel", "Max Pele"
    ), percentage = c(69.5652173913043, 70, 87.2340425531915, 43.75, 
    95, 80), points = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_)), .Names = c("accountid", "firstname", "lastname", 
    "starttime", "value", "value2", "value3", "value4", "startdate", 
    "fullname", "percentage", "points"), row.names = c(NA, 6L), class = "data.frame")