简体   繁体   中英

R code to plot a date range as a bar or line for a number of categorical variables

I am struggling to do this in R. I have a list of station names with two associated variables: Start Date and End Date. What I would like to do is plot a horizontal line or bar chart that ranges from the start and end date for each station name.

I have tried using ggplot, but I'll confess I am recent user to R.

If you have data looking like this (dput is at the end) with

  • start date
  • end date
  • task name or station name
  • and an optional group name

(I invented some data, as the OP does not provide data)

StartDate    EndDate                                    TaskName               Group
1  2018-10-01 2018-11-02                  KPI: high level definition      KPI Definition
2  2018-11-05 2018-11-16                       KPI: data translation      KPI Definition
3  2019-02-18 2019-03-01                          KPI: corroboration      KPI Definition
4  2018-11-05 2018-11-16                KPI: Define Graphical Format      KPI Definition
5  2018-10-22 2018-12-07                            Data: Which data Define and Get Data
6  2018-10-08 2018-10-19                  Data: Mail requesting data Define and Get Data
7  2018-12-07 2018-12-14                    Data: Mail defining data Define and Get Data
8  2018-12-17 2018-12-28                        Data: Test data dump Define and Get Data
9  2018-12-17 2018-12-28                         Data: CSV temporary Define and Get Data
10 2018-12-31 2019-01-25       Data: Quality inspection of Data Dump Define and Get Data
11 2018-12-31 2019-01-25                         Data: Create graphs Define and Get Data
12 2019-01-28 2019-02-15 Data: Correct data comparison with KPI defs Define and Get Data
13 2019-02-04 2019-03-01         Data: Create and publish ppt format Define and Get Data
14 2018-11-19 2018-12-14                              Storage: Where             Storage
15 2018-11-19 2018-12-14                           Storage: How much             Storage

You will need to put it in long format (a separate line for start and end)

library(ggplot2)
library(reshape2) # for melt to get the data in long format

m_planning_data2 <- melt(planning_data2, measure.vars = c("StartDate", "EndDate"))

Then plot it using ggplot:

ggplot(m_planning_data2, aes(value, TaskName)) +
  geom_line(size=4) +
  xlab(NULL) +
  ylab(NULL) +
  ggtitle("Example Assignment Planning 1") +
  theme_minimal() +
  theme(aspect.ratio = 0.4, axis.text = element_text(size = 7))

... yielding this simple plot:

在此输入图像描述

Or plot it with grouping and an annotation for "today"

ggplot(m_planning_data2, aes(value, TaskName, col = Group)) +
  geom_line(size=4) +
  xlab(NULL) +
  ylab(NULL) +
  ggtitle("Example Assignment Planning 2") +
  geom_vline(xintercept = as.POSIXct(as.Date(Sys.time())) , linetype = 1, size=1.5, colour = "purple", alpha= .5) +
  annotate("text", x =  as.POSIXct(as.Date(Sys.time())) + 86400*1.5, y = 3, 
           label = as.Date(Sys.time()), colour = "purple", angle=90, size= 3) + 
    theme_minimal() +
    theme(aspect.ratio = 0.4, axis.text = element_text(size = 7)) 

... yielding the following plot:

在此输入图像描述

Please, let me know whether this is what you were after.

DATA

structure(list(StartDate = structure(c(1538344800, 1541372400, 
1550444400, 1541372400, 1540159200, 1538949600, 1544137200, 1545001200, 
1545001200, 1546210800, 1546210800, 1548630000, 1549234800, 1542582000, 
1542582000), class = c("POSIXct", "POSIXt"), tzone = ""), EndDate = structure(c(1541113200, 
1542322800, 1551394800, 1542322800, 1544137200, 1539900000, 1544742000, 
1545951600, 1545951600, 1548370800, 1548370800, 1550185200, 1551394800, 
1544742000, 1544742000), class = c("POSIXct", "POSIXt"), tzone = ""), 
    TaskName = structure(c(13L, 11L, 10L, 12L, 9L, 6L, 5L, 8L, 
    4L, 7L, 3L, 1L, 2L, 15L, 14L), .Label = c("Data: Correct data comparison with KPI defs", 
    "Data: Create and publish ppt format", "Data: Create graphs", 
    "Data: CSV temporary", "Data: Mail defining data", "Data: Mail requesting data", 
    "Data: Quality inspection of Data Dump", "Data: Test data dump", 
    "Data: Which data", "KPI: corroboration", "KPI: data translation", 
    "KPI: Define Graphical Format", "KPI: high level definition", 
    "Storage: How much", "Storage: Where"), class = "factor"), 
    Group = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 3L, 3L), .Label = c("Define and Get Data", "KPI Definition", 
    "Storage"), class = "factor")), .Names = c("StartDate", "EndDate", 
"TaskName", "Group"), row.names = c(NA, -15L), class = "data.frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM