2013-12-19 77 views
5

我想創建一個由plot類型包裝的ggplot2構面。我已經使用grid.arrange,但我試圖最大化繪圖區域,使用grid.arrange由於y軸和圖例重複出現的鬆散區域。創建繪圖類型的facewrapped的ggplot2圖表

具體來說,我試圖創建一個在右側具有相同數據(如此相同的Y軸)的時間序列的BoxPlot的圖。那麼我會有一個Y軸和一個單一的傳說 - 這可能嗎?

代碼:

library(ggplot2) 
library(gridExtra) 
Time = c("19/12/2013 10:00","19/12/2013 10:01", "19/12/2013 10:02", "19/12/2013 10:03", "19/12/2013 10:04", 
    "19/12/2013 10:05", "19/12/2013 10:06", "19/12/2013 10:07", "19/12/2013 10:08", "19/12/2013 10:09", 
    "19/12/2013 10:10", "19/12/2013 10:11", "19/12/2013 10:12", "19/12/2013 10:13", "19/12/2013 10:14") 

test <- data.frame(Time) 

test$Factor <- c("t1", "t1", "t1", "t1", "t1", "t1", "t1", "t1", "t1", "t2", "t2", "t2", "t2", "t2", "t2") 
test$Values <- c(4,7,2,9,6,9,1,1,5,8,3,4,3,6,7) 
test$PROD <- test$PROD <- c("one", "two", "one", "two", "one", "one", "two", "one", "one", 
      "two", "one", "two", "two", "two", "one") 

p1 <-ggplot(data=test,aes(Factor,Values)) + 
    geom_boxplot(outlier.colour = "red", outlier.size = 3, outlier.shape = 15, fill = "white", colour = "blue") + 
    theme(panel.grid.minor = element_line(colour = "grey"), plot.title = element_text(size = rel(2)),axis.text.x = element_text(angle=90, vjust=1), strip.text.x = element_text(size = 8, colour = "black", face = "bold")) + 
    geom_point(alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0), aes(colour=factor(Factor)), size =3) + 
    facet_wrap(~PROD, scales = "free") + 
    ggtitle("MyTitle") + 
    scale_size_area() + 
    xlab("Tools") + 
    ylab("Values") 

p2<-ggplot(data = test, aes(Time,Values)) + 
    ggtitle("MyTitle") + 
    theme(axis.text.x = element_text(angle=90, vjust=1),plot.title = element_text(size = rel(2))) + 
    geom_point(aes(colour=factor(Factor)), size = 3) + 
    facet_wrap(~PROD, scales = "free") + 
    xlab("TIME") + 
    ylab("Values") 

grid.arrange(p1,p2,ncol=2) 

enter image description here

+2

虛擬磨製變量? https://github.com/hadley/ggplot2/wiki/Align-two-plots-on-a-page – baptiste

+0

@baptiste我不確定這裏可以應用虛擬刻面,因爲Op已經在使用facet_wrap .. – agstudy

+0

@agstudy從概念上講,我認爲一個虛擬變量可以處理這種情況,但它可能不是非常直觀的 – baptiste

回答

1

這不是一個最終的答案,但應該是一個良好的開端。

我會

  • 格式時間軸爲有效日期型
  • 刪除了P1和P2的x軸旋轉的標籤。如果你想保持軸旋轉,你應該調整其他grob以匹配Y軸。
  • 刪除P1圖例
  • 刪除P2 y軸的標籤

例如:

test$Time <- as.POSIXct(test$Time,format='%d/%m/%Y %H:%M') 
## build p1 and p2 using your original code 

grid.arrange(p1+theme(legend.position='none'), 
      p2+theme(axis.title.y=element_blank()), 
      ncol=2) 

enter image description here

EDIT提取圖例GROB有更多的靈活性:

g_legend<-function(a.gplot){ 
    tmp <- ggplot_gtable(ggplot_build(a.gplot)) 
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
    legend <- tmp$grobs[[leg]] 
    legend 
} 
legend1 <- g_legend(p2) 
grid.arrange(p1+theme(legend.position='none'), 
      p2+theme(axis.title.y=element_blank(),legend.position='none'), 
      legend1, 
      ncol=3,nrow=1,widths= c(3/7,3/7,1/7)) 
+0

謝謝 - 對grid.arrange的簡單補充正在做我所需要的,它沒有擺脫第二個Y軸,但從secon陰謀中刪除圖例和y軸標題給我更多的空間。 – PaulBeales

1

你可以玩gtable,但也有缺少一些功能,

library(gtable) 
g1 <- ggplotGrob(p1) 
g2 <- ggplotGrob(p2) 

## drop some elements by name 
## note that trim should check for the sizes if elements still remain 
## in the same column(s)/row(s) of the layout... 
gtable_drop <- function (x, pattern, fixed = FALSE, trim = TRUE) 
{ 
    matches <- grepl(pattern, x$layout$name, fixed = fixed) 
    drop = x$layout[matches, , drop = FALSE] 
    x$layout <- x$layout[!matches, , drop = FALSE] 
    x$grobs <- x$grobs[!matches] 

    if (trim) { 
    x$widths[drop$l] <- replicate(NROW(drop), unit(0,"mm"), simplify=FALSE) 
    x <- gtable_trim(x) 
    } 
    x 
} 

g1p <- gtable_drop(g1, "guide|axis_l-2") 
g2p <- gtable_drop(g2, "axis_l|ylab") 
g <- gtable:::cbind_gtable(g1p, g2p, "last") # ideally "max" but buggy 
grid.newpage() 
grid.draw(g) 

enter image description here