Search
Question: saving a plot as png and also showing inside Rstudio
0
gravatar for mictadlo
12 days ago by
mictadlo0
mictadlo0 wrote:

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
opar <- par(no.readonly = TRUE)
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.

ADD COMMENTlink modified 12 days ago by Aaron Lun16k • written 12 days ago by mictadlo0
1

AFAIK you'll need to run the plotMDS command outside of the png() ... dev.off() structure again to make it visible in RStudio. 

ADD REPLYlink written 11 days ago by Assa Yeroslaviz1.3k

You mean I have to run it twice?

ADD REPLYlink written 11 days ago by mictadlo0
1

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.

ADD REPLYlink modified 11 days ago • written 11 days ago by James W. MacDonald44k
1
gravatar for Aaron Lun
12 days ago by
Aaron Lun16k
Cambridge, United Kingdom
Aaron Lun16k wrote:

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.

ADD COMMENTlink written 12 days ago by Aaron Lun16k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 190 users visited in the last hour