littleworth
littleworth

Reputation: 5169

How to combine two plot objects (non ggplot)

I have the following code:

library(UpSetR)

listInput <- list(one = c(1, 2, 3, 5, 7, 8, 11, 12, 13), two = c(1, 2, 4, 5, 
                                                                 10), three = c(1, 5, 6, 7, 8, 9, 10, 12, 13))

p <- upset(fromList(listInput), order.by = "freq")
jpeg(filename = "test.jpg")
print(p)
dev.off()

Which produces plot like this:

enter image description here

As stated in the plot above I want to add a text MY_TITLE on top of it. I tried this but failed:

t <- grid.text("MY_TITLE", x = 0.65, y = 0.95, gp = gpar(fontsize = 10))
np <- p +  t
jpeg(filename = "test.jpg")
print(np)
dev.off()

What's the right way to do it? Not that I want to store the combined figures into an object. Because I need to do print() to save as file afterward.

Upvotes: 1

Views: 224

Answers (1)

Mr.Rlover
Mr.Rlover

Reputation: 2623

You cannot use ggplot grammar and syntax on base plots. Don't use +, just plot the text code under the print(p) call like so:

jpeg(filename = "test.jpg")
print(p)
grid.text("MY_TITLE", x = 0.65, y = 0.95, gp = gpar(fontsize = 10))
dev.off()

That then gives you what you want.

Upvotes: 1

Related Questions