2014-04-01 43 views
2

有没有一种方法可以使图中较细的线(没有y轴刻度标签的那些线)看起来更靠近上面的线(带有标签的那些线),以便更好地模拟基线/甘特图中相同活动的实际栏? 查看甘特图示例herehere使用ggplot进行甘特图模拟

mdfr <- structure(list(name = structure(c(8L, 8L, 8L, 8L, 6L, 6L, 6L, 
6L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 7L, 7L, 7L, 7L, 5L, 5L, 5L, 
5L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 8L, 8L, 8L, 8L, 6L, 6L, 6L, 
6L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 7L, 7L, 7L, 7L, 5L, 5L, 5L, 
5L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L), .Label = c("100 A", "100 B", 
"101 A", "101 B", "102 A", "102 B", "103 A", "103 B"), class = "factor"), 
    stadio = c(2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 
    1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 2, 4, 5, 
    7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 1, 3, 6, 8, 1, 3, 
    6, 8, 1, 3, 6, 8, 1, 3, 6, 8), variable = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("start_date", "end_date"), class = "factor"), 
    value = c("05/10/2012", "17/12/2012", "12/03/2012", "30/05/2013", 
    "10/01/2013", "14/10/2013", "24/10/2013", "10/01/2014", "30/09/2013", 
    "29/01/2014", "30/01/2014", "06/05/2014", "30/09/2013", "29/01/2014", 
    "30/01/2014", "06/05/2014", "05/10/2012", "17/12/2012", "12/03/2012", 
    "30/05/2013", "10/01/2013", "14/10/2013", "24/10/2013", "10/01/2014", 
    "30/09/2013", "29/01/2014", "30/01/2014", "05/06/2014", "30/09/2013", 
    "29/01/2014", "30/01/2014", "05/06/2014", "17/12/2012", "12/03/2012", 
    "30/05/2013", "30/05/2014", "14/10/2013", "24/10/2013", "10/01/2014", 
    "11/07/2014", "29/01/2014", "30/01/2014", "06/05/2014", "23/12/2014", 
    "29/01/2014", "30/01/2014", "06/05/2014", "23/12/2014", "17/12/2012", 
    "12/03/2012", "30/05/2013", "30/05/2014", "14/10/2013", "24/10/2013", 
    "10/01/2014", "11/07/2014", "29/01/2014", "30/01/2014", "05/06/2014", 
    "28/12/2014", "29/01/2014", "30/01/2014", "05/06/2014", "29/12/2014" 
    ), rating = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("3", 
    "5"), class = "factor")), row.names = c(NA, -64L), .Names = c("name", 
"stage", "variable", "value", "rating"), class = "data.frame") 

names <- as.character(unique(mdfr$name)) 
names1 <- names[gsub("[^ B]","",names) == " B"] 
names1 <- paste("No.",gsub("[ B]","",names1),sep="") 
names2 <- rep("",length(names1)) 
new.names <- c(names1,names2) 

ggplot(mdfr, aes(as.POSIXct(as.Date(value, "%d/%m/%Y")), name, colour = factor(stage))) + 
    geom_line(aes(size=rating)) + 
    labs(colour="(Baseline/Actual):", x = "", y = "") + 
    scale_colour_brewer(palette="RdYlGn",breaks = c("1", "3", "6","8"), guide = "none") +      
    scale_size_manual(breaks = levels(mdfr$rating), values = as.integer(levels(mdfr$rating)), guide = "none") + 
scale_y_discrete(breaks=names, labels=new.names) 

回答

1

我会用facet来做到这一点。下面你找到一个可能的解决方案这可能不是最优雅的解决方案,但它可以让您通过更改scale_x_discrete中的expand参数来更改较细线和较粗线之间的距离。

# numbers to facet by (levels used for order of the facets) 
mdfr$nr <- factor(paste0("No.", as.numeric(gsub("A|B", "", mdfr$name))), 
        levels=unique(paste0("No.", as.numeric(gsub("A|B", "", mdfr$name))))) 
# recast your data 
df <- dcast(mdfr, nr+stage+rating~variable) 
# plot as before, switched x and y values 
ggplot(df, aes(x=factor(rating), 
       ymin=as.POSIXct(as.Date(start_date, "%d/%m/%Y")), 
       ymax=as.POSIXct(as.Date(end_date, "%d/%m/%Y")), 
       color=factor(stage), 
       size=rating 
       )) + 
    geom_linerange() + # linerange instead of line 
    facet_grid(nr~., scales="free_x") + # faceting 
    coord_flip() + # flip coordinates back 
    scale_x_discrete(name="", breaks=NULL, expand=c(4,1)) + # use the expand variable to change the distances 
    scale_colour_brewer(palette="RdYlGn",breaks = c("1", "3", "6","8"), guide = "none") + 
    scale_size_manual(breaks = levels(mdfr$rating), values = as.integer(levels(mdfr$rating)), guide = "none") 
+0

这是一个不错的主意!标签是否可以水平放置并显示在左侧? –

+1

我不认为这是可能的。最接近这个我可以找到的引用是以下链接:https://github.com/hadley/ggplot2/wiki/labeller – shadow