2015-04-02 152 views
0

我想繪製兩行字符在與ggplot2相同的情節。我知道你可以用grid.arrange來做,但我肯定會在代碼中丟失一些東西。ggplot2:爲什麼grid.arrange不工作?

這裏是我的數據,在這裏我想繪製了時間和速率

motor;trace;time;rate 
monetDBIE;1m;5448;183553 
monetDBIE;2m;8734;228990 
monetDBIE;5m;19468;256831 
Jena;1m;8273;120875 
Jena;2m;16916;118231 
Jena;5m;44776;111666 
PGSQLIE;1m;6859;145793 
PGSQLIE;2m;13106;152601 
PGSQLIE;5m;32793;152471 

這裏是我的代碼

require("ggplot2") 
require("gridExtra") 

w <- read.csv(file="loading.csv", head=TRUE, sep=";") 

# first plot 
p <- ggplot(data=w, aes(x=trace, y=time, colour=motor, shape=motor)) 

p <- p + geom_point(size=4) 

p <- p + geom_line(size=1,aes(group=motor)) 

p <- p + geom_text(aes(label=time), hjust=-0.2, vjust=1) 

p <- p + ggtitle("Triplestores ") 

p <- p + labs(x = "Traces", y = "Temps (ms)") 

p <- p + theme_bw() 

# second plot 
p2 <- ggplot(data=w, aes(x=trace, y=rate, colour=motor, shape=motor)) 

p2 <- p2 + labs(x = "Traces", y = "débit de chargement (triplets/s)") 

p2 <- p2 + geom_point(size=4) 

p2 <- p2 + geom_line(size=1,aes(group=motor)) 

p2 <- p2 + geom_text(aes(label=rate), hjust=-0.2, vjust=1) 

p2 <- p2 + theme_bw() 

grid.arrange(p, p2, nrow=2, ncol=1) 

ggsave(file="loading.pdf") 

但不幸的是我仍然只得到一個字符P2看到的畫面enter image description here

爲什麼?

回答

0

grid.arrangepdf這樣的:

library(ggplot2) 
library(gridExtra) 
p1 <- p2 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar() 
pdf(tf <- tempfile(fileext = ".pdf")) 
grid.arrange(p1, p2) 
dev.off() 
shell.exec(tf) 
+0

您的回答我不清楚。什麼是'p1','factor(cyl)','mtcars'? – 2015-04-02 16:06:37

+0

'p1'和'p2'是兩個任意的ggplot對象。什麼是原始問題 - 你想在pdf中使用兩個網格佈置的圖嗎?也許我沒有找到你.. – lukeA 2015-04-02 16:09:38

0

在我看來,一個更好的解決辦法是避免grid.arrange乾脆簡單地使用arrangeGrob,它允許你使用ggsave功能的靈活性。

big_plot <- arrangeGrob(p, p2, nrow=2, ncol=1) 
print(big_plot) 

ggsave(big_plot, ...) 
+0

請注意'ggsave'的第一個參數是文件名,而不是劇情。 – baptiste 2015-04-02 22:04:13