CellChat icon indicating copy to clipboard operation
CellChat copied to clipboard

How to save multiple plots produced by netVisual_circle?

Open lwlive opened this issue 2 years ago • 3 comments

Dear,
when I use cellchat ,I am wondering how to save multiple plots which produced from following code: par(mfrow = c(1,2), xpd=TRUE) netVisual_diffInteraction(cellchat, weight.scale = T) netVisual_diffInteraction(cellchat, weight.scale = T, measure = "weight")

I have tried following commands but only one figure in the png file. png("interaction_count_and_strength_circle2.png,width = 6007, height = 6003, res=300) par(mfrow = c(1,2)) netVisual_circle(cellchat@net$count, vertex.weight = groupSize, weight.scale = T,margin=0, label.edge= F, title.name = "Number of interactions") netVisual_circle(cellchat@net$weight, vertex.weight = groupSize, weight.scale = T,margin=0, label.edge= F, title.name = "Interaction weights/strength") dev.off()

can you offer some help?

Thanks! Wei

lwlive avatar Jul 30 '22 10:07 lwlive

@lwlive Have you tried grDevices::pdf ... dev.off() or 'grDevices::cairo_pdf ... dev.off()'

sqjin avatar Aug 05 '22 16:08 sqjin

Hello, I have been struggling with such issue all afternoon, but after some research online and on the 'issues' forum of sqjin/CellChat, I have found a simple solution. So I share it here for other people that might have the same problem in the future.

library(extrafont)
List_of_pathways = sort(cellchat@netP$pathways) #or any other list of pathways you are interested in (could be only 2 pathways for example).
#All pathways on the same graph
png(filename = "All_pathways.png", bg = "transparent", width = 2000, height = 1250)
par(mfrow = c(5, 7), xpd = TRUE)
for (i in List_of_pathways) {
	netVisual_aggregate(object = cellchat, layout = "circle", signaling = as.character(i))
}
dev.off()
#Individual pathways (put in one folder named outs)
for (i in List_of_pathways) {
	png(filename = paste("outs/", as.character(i), ".png", sep = ""), bg = "transparent", width = 750, height = 750)
	par(mfrow = c(1, 1), xpd = TRUE)
	netVisual_aggregate(object = cellchat, layout = "circle", signaling = as.character(i))
	dev.off()
}

Here in the code above, I export my images in the png format, but it also works for pdf of course.

Here in the code above, I use the netVisual_aggregate() function, but it works with the other similar functions of the CellChat package (I have not tried all of them to be honest though).

Note that I had to load the extrafont library to get the titles appeared correctly according to #24 (and also in #18 ).

I am not an expert of the par() function usage, but I have noticed that png() or pdf() or cairo_pdf() must always be BEFORE par(mfrow = c(X, Y), xpd = TRUE), otherwise it will cause problems. For example, when using pdf(), it creates one pdf with each pathway on a different page of the pdf...

Anyway, I hope it will help you. Best regards,

CroixJeremy2 avatar Jan 21 '24 15:01 CroixJeremy2

@CroixJeremy2 Thank you!

sqjin avatar Jan 22 '24 11:01 sqjin