r - 在 R 中绘制年度计划

标签 r ggplot2

我一直在尝试扩展 my own answer on drawing Gantt Charts到更一般的情况。我在这里说明了部分结果:

Year planner in R

此图表上绘制了多次行程,红点代表出发,绿点代表返回。现在这是我的挑战:

  • 我想用灰色点标记出发和返回之间的所有日子,而让所有其他人保持不变。
  • 我希望这些点能够正确地环绕不同的月份长度(例如,6 月 28 日出发会将日期标记为 6 月 30 日,然后环绕到 7 月 1 日)。
  • 在一日游的情况下,脚本不应失败(例如 9 月初的旅行,出发和返回发生在同一天,较小的绿点绘制在较大的红色标记上)。

  • 用下面的代码生成这个图很简单,而且很容易用一条从红色到绿色的线连接点,两者都发生在同一个月。任何人都可以以一种足以承受闰年和非闰年等的一般方式帮助解决环绕的情况吗?
    library("ggplot2")
    
    # ----------------
    # LOAD DATA
    
    df <- read.table(text='id,dep_month,dep_day,ret_month,ret_day
    c,1,5,1,16
    v,2,1,2,6
    a,3,28,3,31
    z,4,9,4,11
    y,4,25,5,3
    f,6,28,7,7
    w,8,19,8,29
    b,9,9,9,9
    k,9,29,10,6
    n,11,20,12,3', header = TRUE,
                     sep = ',')
    
    # ----------------
    # DRAW YEAR CHART
    
    p <- ggplot(data = df,
                aes(x = dep_day,
                    y = dep_month
                    )
                )
    p <- p + theme_bw()
    p <- p + geom_point(colour = 'red',
                        size = 6)
    p <- p + geom_point(aes(x = ret_day,
                           y = ret_month
                           ),
                       colour = 'green',
                       size = 4
                        )
    p <- p + scale_x_continuous( breaks = c(1:31) )
    p <- p + scale_y_reverse( breaks = c(1:12) )
    p <- p + ylab("Month") + xlab("Day")
    print(p)
    

    最佳答案

    可能不是完美的解决方案,但是当您使用 date 时,您会认为变得容易得多。对象:

    # using your data.frame
    # create date object using the dep_month, dep_day etc columns
    df$dep.date = as.Date(paste('2012', df$dep_month, df$dep_day, sep='-'))
    df$ret.date = as.Date(paste('2012', df$ret_month, df$ret_day, sep='-'))
    
    # calculate the dates for the gray data points between departure and return
    # there is probably a more R'ish ways, than a for loop
    days <- NULL
    for(i in seq(1, nrow(df))){
        tmp <- seq.Date(df[i,]$dep.date, df[i,]$ret.date, by='days')
        days <- rbind(days,
            data.frame(day = as.POSIXlt(tmp)$mday,
                mon = as.POSIXlt(tmp)$mon+1))
    }
    
    # plot it
    p <- ggplot( days, aes(day, mon)) + geom_point(colour='gray66') + theme_bw() +
     geom_point(data = df, aes(dep_day, dep_month), colour = 'red', size = 6) +
     geom_point(data = df, aes(ret_day, ret_month ),
                   colour = 'green', size = 4 )  +
     scale_x_continuous( breaks = c(1:31) ) +
     scale_y_reverse( breaks = c(1:12) ) +
     ylab("Month") + xlab("Day")
    print(p)
    

    enter image description here

    关于r - 在 R 中绘制年度计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10537500/

    相关文章:

    根据第二个数据框列中的匹配替换数据框列中的值

    r - 使用 plyr 计算跨组的年同比变化的初学者提示

    r - 将十六进制字符串值硬编码为 ggplot 中的颜色

    r - 向甘特图添加阴影以描绘周末

    r - 带逗号但没有小数的轴标签ggplot

    r - 如何在 R 中使用 ggplot 有条件地填充区域

    r - 根据确切的行/列名称插入矩阵值

    r - R中结果为负时显示2位数字和错误消息

    R ggplot : Change spacing between two different legends

    r - 将月份添加到 R 中 data.table 的 IDate 列