简体   繁体   English

R代码将日期范围绘制为多个分类变量的条形或线条

[英]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. 我在R中努力做到这一点。我有一个包含两个相关变量的站名列表:开始日期和结束日期。 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. 我尝试过使用ggplot,但我会承认我是R的最近用户。

If you have data looking like this (dput is at the end) with 如果你有这样的数据(dput在最后)

  • 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) (我发明了一些数据,因为OP不提供数据)

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绘制它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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