1
votes

I want to plot two lines char in a same plot with ggplot2. I'm aware that you can do it with grid.arrange but I certainly missing something in my code.

Here are my data, where I want to plot both time and rate

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

And here is my code

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")

But unfortunatly I still get only one char p2 see the picture enter image description here

Why ?

2

2 Answers

2
votes

Wrap grid.arrange in pdf like this:

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
votes

a better solution in my opinion is to avoid grid.arrange altogether and simply use arrangeGrob, which allows you the flexibility of using the ggsave function.

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

ggsave(big_plot, ...)