How to modify my R code to plot this kind of gantt chart?

你说的曾经没有我的故事 提交于 2020-03-15 06:03:20

问题


everybody

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

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;

to plot such a gantt chart

Thanks a lot, for any reply !


回答1:


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

Produces:

EDIT to produce centred labels

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

Which in turn produces:




回答2:


using package vistime:

install.packages('vistime')
library(vistime)
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$Start <- as.Date("2017-01-01") + df$Start
df$End <- as.Date("2017-01-01") + df$End
vistime(df, start="Start", end="End", events="Task")

https://github.com/shosaco/vistime



来源:https://stackoverflow.com/questions/10489411/how-to-modify-my-r-code-to-plot-this-kind-of-gantt-chart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!