如何修改我的 R 代码来绘制这种甘特图? [英] How to modify my R code to plot this kind of gantt chart?

查看:51
本文介绍了如何修改我的 R 代码来绘制这种甘特图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家

在 R 中使用以下代码,我显示了一个简单的甘特图:

with the following code in R, I have display a simple gantt chart :

dat <- read.csv2(text="start;duration
 1;4
 7;3
 15;2
 ")
 plot(NA, xlim=c(0,20), ylim=c(0,9), ylab="",xlab="X", xaxt="n", yaxt="n")
 with(dat, segments(x0=start, x1=start+duration, y0=2,y1=2))
 with(dat, text( start+duration/2, 2.5, labels=duration))
 axis(1, at=seq(0,20,by=2), labels=seq(0,20,by=2))

现在我如何修改此代码以能够从 csv 文件中的这些数据:

Now How could I modify this code to be able from these data in a csv file :

A; 2; 7;
B; 5; 10;
C; 5; 12;
D; 16; 22;
E; 18; 20;

绘制这样的甘特图

非常感谢,任何回复!

推荐答案

扩展@Tyler Rinker 建议的答案:

Extending the answer suggested by @Tyler Rinker:

library(ggplot2)

df <- read.table(text="Task, Start, End
A,2,7
B,5,10
C,5,12
D,16,22
E,18,20", 
                 header=TRUE,
                 sep = ',')

p <- ggplot(df, aes(colour=Task))
p <- p + theme_bw()
p <- p + geom_segment(aes(x=Start, 
                          xend=End, 
                          y=Task, 
                          yend=Task), 
                      size=2)
p <- p + geom_point(aes(x=Start,
                        y=Task),
                    size=5)
p <- p + geom_point(aes(x=End,
                        y=Task),
                    size=5)
p <- p + geom_text(aes(x=Start-0.5,
                       y=Task,
                       label=Task),
                   fontface="bold")
p <- p + opts(legend.position="None",
              panel.grid.major = theme_blank(),
              axis.text.y = theme_blank())
p <- p + xlab("Duration")
p

产生:

编辑以生成居中标签

library(ggplot2)

df <- read.table(text="Task, Start, End
A,2,7
B,5,10
C,5,12
D,16,22
E,18,20", 
                 header=TRUE,
                 sep = ',')

df$TaskLabel <- df$Task
df$Task <- as.numeric(df$Task)

p <- ggplot(df, aes(colour=TaskLabel))
p <- p + theme_bw()
p <- p + geom_segment(aes(x=Start, 
                          xend=End, 
                          y=Task, 
                          yend=Task), 
                      size=2)
p <- p + geom_point(aes(x=Start,
                        y=Task),
                    size=5)
p <- p + geom_point(aes(x=End,
                        y=Task),
                    size=5)
p <- p + geom_text(aes(x=(Start+End)/2,
                       y=Task+0.25,
                       label=TaskLabel),
                   fontface="bold")
p <- p + opts(legend.position="None",
              panel.grid.major = theme_blank(),
              axis.text.y = theme_blank())
p <- p + xlab("Duration")
p

依次产生:

这篇关于如何修改我的 R 代码来绘制这种甘特图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆