日期格式 Ggplot2 折线图 R

Date format Ggplot2 linegraph R

我想以 2018-03-28 格式输出日期。当我尝试这样做时,我只得到折线图中的点:

当使用as.Date或strptime(无所谓)时,它只给出:jan,feb,mar,apr。但是线路正在工作:

这是我的代码,用于获取与 1 月、2 月、3 月一起使用的行。

  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
    )

当运行命令时:dput(head(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")

你可以试试这个

#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)


您的第一个情节的问题是 rsShotResult$starttime 属于 class character。在绘图时,您应该已经阅读了以下内容

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

因此您也可以使用此代码:

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

但是,这会导致离散比例,我不建议以这种方式绘制数据,因为日期是连续的。但这当然取决于你。

希望对您有所帮助。

数据

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")