saving a plot as png and also showing inside Rstudio
1
0
Entering edit mode
Last seen 2.9 years ago

Hi, the following plot get saved as png but it does not show up inside Rstudio.

png("~/sampledata.png")
x <- matrix(rnorm(1000*6,sd=sd),1000,6)
rownames(x) <- paste("Gene",1:1000)
x[1:50,4:6] <- x[1:50,4:6] + 2
## capture par settings, then add space to the right
par(xpd = TRUE, mar = par()$mar + c(0, 0, 0, 5)) ## get yer plot on plotMDS(x, pch = 16, col = c(rep("black",3), rep("red",3))) legend(par("usr")[2], par("usr")[4], c("Grp1","Grp2"), pch = 16, col = c("black","red"), bty = "n") ## set par back to original par(opar) dev.off()  How is is possible to store the plot as png and show it inside Rstudio? Thank you in advance. edger ggplot2 • 1.9k views ADD COMMENT 1 Entering edit mode AFAIK you'll need to run the plotMDS command outside of the png() ... dev.off() structure again to make it visible in RStudio. ADD REPLY 0 Entering edit mode You mean I have to run it twice? ADD REPLY 1 Entering edit mode This has nothing to do with Rstudio. When you plot something in R, the output is captured by a device. If you start with a call to png, then you are capturing by the png device so you can generate a plot. If you just run the code, it gets captured by the graphics device and is plotted in a window. You can always capture the plot separately and then replay it for different devices, however. sd <- 0.3*sqrt(4/rchisq(1000,df=4)) x <- matrix(rnorm(1000*6,sd=sd),1000,6) opar <- par(no.readonly = TRUE) par(xpd = TRUE, mar = par()$mar + c(0, 0, 0, 5))

plotMDS(x, pch = 16, col = c(rep("black",3), rep("red",3)))
legend(par("usr")[2], par("usr")[4], c("Grp1","Grp2"), pch = 16, col = c("black","red"), bty = "n")
z <- recordPlot()

png("tmp.png")
replayPlot(z)
dev.off()

par(opar)

But do note that the converse (running within png, then trying to capture using recordPlot) doesn't work.

1
Entering edit mode
Aaron Lun ★ 28k
@alun
Last seen 16 hours ago
The city by the bay

Your issue seems to be with Rstudio, and this support site is not an appropriate forum to answer such questions. All I can say is that plotMDS is doing its job correctly - where you want the image to go is up to you.