Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign plots to a variable. #100

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

G-Thomson
Copy link

Hi

I really like Upset plots and am using them in my work. However as part of a reproducible workflow I would like to be able to assign the plots to variables which can then be called later and if required incorporated into more complex figures.

I realise that this was not an intended use case when UpsetR was originally created given the composite nature of the plots but looking through the code and with help from @pmur002 we have come up with a solution. I have renamed all the grobs created in Upset.plot.R with unique descriptive names which allow grid.grab() to distinguish between the components and select them all into a gTree object which I then called at the end of upset().

This solution retains all previous functionality but now allows the assigning of plots to variables. This script is a example of what I mean using my adapted code:

# Example data
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))

# Produce chart
upset(fromList(listInput), order.by = "freq")

# Grobs renamed
grid.ls()

# Assign to variable - creates gTree object
example <- upset(fromList(listInput), order.by = "freq")
grid.newpage() # Clear viewer
example

# Can plot by drawing gTree object
grid.draw(example)

# e.g. useful when creating figures 
library(ggpubr) # Package avaliable from CRAN
ggarrange(example, example, ncol = 2, labels = c("A", "B"))

@aphalo
Copy link

aphalo commented Dec 5, 2017

I have started using UpSetR recently, and I was badly missing this feature. I hope this pull request gets merged soon!

@aphalo
Copy link

aphalo commented Dec 5, 2017

It would be good to check how this works within knitr and Rmd files before merging. I played briefly with these commits and even if saving to a variable is possible, a suitable print method would also need to be defined. I am not sure how one should go about building and printing the nested ggplots .

@G-Thomson
Copy link
Author

There does seem to be some quirks with the graphical outputs of R notebooks when using the grid package (see here) but grid.draw() does appear to work if you put grid.newpage() immediately before it.

Knitr seems to work ok (see this file) and when you call grid.draw()and you get a single plot.

However due to the nature of the way Upset plots are generated using it on its own in R notebooks and knitr documents results by not assigning to a variable you get multiple plots.

While important and worth looking into I think this is a different issue to what I'm trying to address with this pull request.

@alanocallaghan
Copy link
Contributor

I have an alternative implementation for this in #101, I simply hope one of the two gets merged

@bimbam23
Copy link

is it now possible to save plots into vars? For me it does not work, the result is empty.

@alanocallaghan
Copy link
Contributor

It is possible, just not on the CRAN version.

install_github("hms-dbmi/UpSetR")
library("UpSetR")

movies <- read.csv( system.file("extdata", "movies.csv", package = "UpSetR"), header=T, sep=";" )

u <- upset(movies)

summary(u)
print(u)

@bimbam23
Copy link

bimbam23 commented Feb 1, 2019

It is possible, just not on the CRAN version.

install_github("hms-dbmi/UpSetR")
library("UpSetR")

movies <- read.csv( system.file("extdata", "movies.csv", package = "UpSetR"), header=T, sep=";" )

u <- upset(movies)

summary(u)
print(u)

Thanks now it works. Great!
I have an other question: Is it possible to use cowplot or any other method to plot
multiple plot in a grid?

# e.g. useful when creating figures library(ggpubr) # Package avaliable from CRAN ggarrange(example, example, ncol = 2, labels = c("A", "B"))

does not work

@bimbam23
Copy link

bimbam23 commented Feb 1, 2019

I found a rather complicated solution but it works (idea from here):

upset_p1
grid.edit('arrange',name='arrange2')
vp1 = grid.grab()
upset_p2
grid.edit('arrange',name='arrange2')
vp2 = grid.grab()
upset_p3
grid.edit('arrange',name='arrange2')
vp3 = grid.grab()
upset_p4
grid.edit('arrange',name='arrange2')
vp4 = grid.grab()
svg("4upset_plots.svg", width = 12, height = 9)
plot_grid(vp1,vp2,vp3,vp4, ncol = 2, labels = c('A', 'B', 'C', 'D'))
dev.off()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants