2014-04-01 4 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

나는이를 위해 패싯을 사용합니다. 아래에서 가능한 해결책을 찾으십시오. 이것은 가장 우아한 해결책은 아니지만 expand 인수를 scale_x_discrete으로 변경하여 더 얇은 선과 더 두꺼운 선 사이의 거리를 변경할 수 있습니다.

# 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

관련 문제