2

While plotting in R in QGIS, only the latest plot appears in 'Result viewer'. What do I do to get all the plots done during script running? Here's the code:

##showplots

bin_arr <- c("C:/Users/Madhur/Desktop/Screenshot_1.png", "C:/Users/Madhur/Desktop/Screenshot_2.png")

shp_arr <- c("C:/Users/Madhur/Desktop/SAR_Calibration/Screenshots_SAR/final.png", "C:/Users/Madhur/Desktop/SAR_Calibration/Screenshots_SAR/Screenshot_2.png")

library(imager)

im1<-load.image(bin_arr[2])
plot(im1)

im2<-load.image(shp_arr[1])
plot(im2)

im<-load.image(shp_arr[2])
plot(im)

I only get the plot corresponding to the last plot statement i.e. plot(im). I don't know if this is related or not but I ran the same script on cmd using Rscript script-name.R and not a single plot appeared. Please help me show multiple plots in QGIS.

0

2 Answers 2

1

To create a new window for each plot, use the windows() command. Editing your code to the following should create each plot without overwriting the previous results:

##showplots

bin_arr <- c("C:/Users/Madhur/Desktop/Screenshot_1.png", "C:/Users/Madhur/Desktop/Screenshot_2.png")

shp_arr <- c("C:/Users/Madhur/Desktop/SAR_Calibration/Screenshots_SAR/final.png", "C:/Users/Madhur/Desktop/SAR_Calibration/Screenshots_SAR/Screenshot_2.png")

library(imager)

im1<-load.image(bin_arr[2])
windows(); plot(im1)

im2<-load.image(shp_arr[1])
windows(); plot(im2)

im<-load.image(shp_arr[2])
windows(); plot(im)
6
  • Thanks for that. The windows do show up but they show up really quickly and then disappear. I am getting same results in QGIS and cmd both. In fact in QGIS, now I am not even getting the last plot. I get the Result viewer window with empty plot. How do I make these windows stay. Commented Jul 5, 2018 at 21:41
  • Hmm... I'm not sure why the windows would close right after they're created. It must have something to do with how QGIS handles R graphical output. In R, using the windows() command opens new persistent windows that stay open and do not overwrite each other.
    – lambertj
    Commented Jul 5, 2018 at 22:03
  • But even if I run the script in command prompt, the windows disappear. So it is not about how QGIS handles R plots. There must be some way to do this. Pleasr update here if you find something out. Also, the windows do not go as soon as created. Actually, all the windows stay until the last plot window is created and then they disappear(although that is a split second as well but just for the sake of clarity). Commented Jul 6, 2018 at 3:55
  • OK, that's a little different than I thought. Have you tried running your script directly in R instead of QGIS or cmd? That would tell us if it's a problem with how the other programs handle R graphical windows or if it's a problem with your code.
    – lambertj
    Commented Jul 6, 2018 at 13:53
  • I actually use R studio and everything is fine there. I get all the plots in the 'plots' section. Since I am making a script for detecting oil spills in a SAR image, which requires shapefile inputs,etc so it is better suited to run in QGIS itself. That is why I need to know the solution of this plot rendering problem. Since QGIS and cmd are behaving similarly, I assume that if I can make the plot appear in cmd, it'd be in QGIS as well. Commented Jul 6, 2018 at 16:26
0

In fact, I cannot display many graphics. Only the last one, as you say. I arranged a way to store my graphics in files, then I am able to see all of them.

My code is very different than yours, but you will be able to find out.

##showplots
require(ggplot2)  
Size = 10
x = 1:Size
y1 = x^2
y2 = x^3

>print('Graphcis are in this directory')
>getwd()


png('y1-plot.png', 800,800)
plot(x,y1)
dev.off()
    
png('y2-plot.png', 800,800)
plot(x,y2)
dev.off()

ggplot(data.frame(x,y1))+
  aes(x=x, y=y1) +
  geom_line()
ggsave('y1-ggplot.png')

ggplot(data.frame(x,y2))+
  aes(x=x, y=y2) +
  geom_line()
ggsave('y2-ggplot.png')

In my directory, I can see four graphics.

y1-ggplot.png y1-plot.png y2-ggplot.png y2-plot.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.