1

I have been trying to draw a bar plot for project scheduling (like gantt chart) in which time and cost for a different task are different. I want to code in R that gives bar plot corresponding to time. It must also reflect different cost for each task (say giving different colors after grouping the task with certain cost range like red for 0-0.2m$, green for 0.2-0.4m$,... ).

The dataset attached below has "Link" as task and column as different time periods. 0 is no time and cost while any positive value is cost associated with task for that time period. (For example, link 1 has 0.34m $ cost and it requires only one time period which is 13th)

So far, I have written down the following code to convert matrix form into long format, to read data in bar plot function. I could not go forward from this point so any help would be appreciated!

my code:

library(readr)
library(reshape2)
data_mat<-read_csv("staging.csv")

#reshape from wide to long
data_ijv<- melt(data_mat, id.vars = "Link", variable.name = "Time_period", 
                value.name = "cost")

#order TAZ column
staging<-data_ijv[order(data_ijv$Link),]

barplot(staging$Link[staging$cost])--(How to program here?)

dput(data_mat)

structure(list(Link = 1:40, `1` = c(0, 0, 0, 0.36, 0.14, 0, 0, 
0.64, 0.58, 0.24, 0.56, 0.2, 0, 0, 0, 0.536, 0.2, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0.546666667, 0, 0.5, 0, 0.54, 0, 0, 0, 0.36, 0, 
0.38, 0.613333333, 0.58, 0), `2` = c(0, 0, 0, 0, 0, 0, 0, 0, 
0.58, 0, 0.56, 0, 0, 0, 0, 0.536, 0, 0.6, 0, 0, 0, 0, 0, 0, 0, 
0, 0.546666667, 0.655, 0.5, 0, 0.54, 0.64, 0, 0, 0.36, 0, 0.38, 
0.613333333, 0.58, 0), `3` = c(0, 0, 0.34, 0, 0, 0, 0, 0, 0.58, 
0, 0.56, 0, 0, 0, 0, 0.536, 0, 0.6, 0, 0, 0.606666667, 0.643076923, 
0, 0, 0.62, 0, 0.546666667, 0.655, 0, 0, 0, 0.64, 0, 0, 0, 0, 
0, 0.613333333, 0, 0), `4` = c(0, 0, 0, 0, 0, 0, 0.57, 0, 0.58, 
0, 0.56, 0, 0, 0, 0.582857143, 0.536, 0, 0.6, 0, 0.633333333, 
0.606666667, 0.643076923, 0, 0, 0.62, 0, 0, 0.655, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), `5` = c(0, 0, 0, 0, 0, 0, 0.57, 0, 0.58, 
0, 0, 0, 0, 0, 0.582857143, 0.536, 0, 0, 0, 0.633333333, 0.606666667, 
0.643076923, 0, 0, 0.62, 0, 0, 0.655, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0.647272727), `6` = c(0, 0.665, 0, 0, 0, 0, 0.57, 0, 
0.58, 0, 0, 0, 0, 0, 0.582857143, 0, 0, 0, 0, 0.633333333, 0.606666667, 
0.643076923, 0, 0, 0.62, 0, 0, 0.655, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0.647272727), `7` = c(0, 0.665, 0, 0, 0, 0, 0.57, 0, 
0.58, 0, 0, 0, 0, 0, 0.582857143, 0, 0, 0, 0, 0.633333333, 0.606666667, 
0.643076923, 0, 0, 0.62, 0.62, 0, 0.655, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0.647272727), `8` = c(0, 0.665, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0.582857143, 0, 0, 0, 0, 0.633333333, 0.606666667, 
0.643076923, 0.58, 0, 0.62, 0.62, 0, 0.655, 0, 0, 0, 0, 0, 0.62, 
0, 0, 0, 0, 0, 0.647272727), `9` = c(0, 0.665, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0.56, 0, 0.582857143, 0, 0, 0, 0, 0.633333333, 
0, 0.643076923, 0.58, 0, 0.62, 0.62, 0, 0.655, 0, 0, 0, 0, 0, 
0.62, 0, 0, 0, 0, 0, 0.647272727), `10` = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0.582857143, 0, 0, 0, 0, 0.633333333, 
0, 0.643076923, 0, 0.651428571, 0.62, 0.62, 0, 0, 0, 0.44, 0, 
0, 0.611428571, 0.62, 0, 0, 0, 0, 0, 0.647272727), `11` = c(0, 
0, 0, 0, 0, 0.58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.633333333, 
0, 0.643076923, 0, 0.651428571, 0, 0.62, 0, 0, 0, 0, 0, 0, 0.611428571, 
0.62, 0, 0.65, 0, 0, 0, 0.647272727), `12` = c(0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.633333333, 0, 0.643076923, 
0, 0.651428571, 0, 0.62, 0, 0, 0, 0, 0, 0, 0.611428571, 0.62, 
0, 0.65, 0, 0, 0, 0.647272727), `13` = c(0.34, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.606666667, 0.633333333, 
0, 0.643076923, 0, 0.651428571, 0, 0.62, 0, 0, 0, 0, 0, 0, 0.611428571, 
0.62, 0, 0.65, 0, 0, 0, 0.647272727), `14` = c(0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.606666667, 0.633333333, 
0, 0.643076923, 0, 0.651428571, 0, 0.62, 0, 0, 0, 0, 0, 0, 0.611428571, 
0.62, 0, 0.65, 0, 0, 0, 0.647272727), `15` = c(0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.606666667, 0.633333333, 
0, 0.643076923, 0, 0.651428571, 0, 0, 0, 0, 0, 0, 0, 0, 0.611428571, 
0.62, 0, 0.65, 0, 0, 0, 0.647272727), `16` = c(0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.651428571, 
0, 0, 0, 0, 0, 0, 0, 0, 0.611428571, 0, 0, 0.65, 0, 0, 0, 0.647272727
), `17` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.65, 0, 
0, 0, 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, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0.647272727), `26` = c(0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.647272727), `27` = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `28` = c(0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L), `29` = c(0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L), `30` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-40L), .Names = c("Link", "1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
"30"))

santosh
  • 19
  • 6
  • You should use the **R** tag, not the RStudio one. And your question is not very clear, you should be more precise. One last tip: include your data in your question with `dput` instead of linking to an image. – Scarabee Feb 15 '17 at 02:26
  • @Scarabee: Thanks for suggestion. I have attached the dput format now. The values indicate the cost associated and their presence (for a particular row) under time column represent the period it takes to complete task while 0 indicate no cost and time allocation. Hope that helps. Please let me know on any confusion. – santosh Feb 15 '17 at 17:11
  • @Scarabee: I think it makes a lot more sense now! – santosh Feb 17 '17 at 02:32
  • Great, it does make more sense. Did you have a look at other questions such as [this one](http://stackoverflow.com/questions/3550341/gantt-charts-with-r)? – Scarabee Feb 17 '17 at 07:56
  • @Scarabee: Thanks for the link! – santosh Feb 17 '17 at 14:50
  • @Scarabee: Ya but this does not help me as my dataset type is different and I cannot change my data manually as it is large. I have attached only the sample here. – santosh Feb 17 '17 at 15:42

0 Answers0