如何在R中保存生成的图片而不是网页的形式?

时间:2016-10-15 15:20:16

标签: r

这是我的代码:

provname=c("CN-11","CN-12","CN-13","CN-14","CN-15",
    "CN-21","CN-22","CN-23","CN-31","CN-32",
    "CN-33","CN-34","CN-35","CN-36","CN-37",
    "CN-41","CN-42","CN-43","CN-44","CN-45",
    "CN-46","CN-50","CN-51","CN-52","CN-53",
    "CN-54","CN-61","CN-62","CN-63","CN-64","CN-65");
    pop=c(110.56,112.51,113.43,112.52,108.45,112.83,111.23,109.71,110.64,116.51,
      113.86,127.85,117.93,114.74,112.17,118.46,128.18,126.16,130.30,
      125.55,135.64,115.13,116.01,107.03,108.71,102.73,122.10,114.82,
      110.35,108.79,106.12)
    ## 
    library(googleVis)
    a<-data.frame(provname,pop)
    G1 <- gvisGeoMap(a, locationvar='provname', numvar='pop',options=list(region='CN'))
    plot(G1) 
    ##
    G2 <- gvisGeoChart(a, locationvar='provname', colorvar='pop',options=list(region='CN',displayMode="regions",resolution="provinces",colorAxis="{colors: ['yellow','red']}" ))
    plot(G2) 

1 个答案:

答案 0 :(得分:1)

Take a look at the webshot package. The plot method for googleVis objects returns the path to the HTML file. Save that into a variable and you can do:

library(webshot)

out <- plot(G2)
webshot(out, delay=2)

enter image description here

You can customize the height, width, clipping region, etc (and, as you can see, you prbly will need to).

You may also need to adjust delay on your system depending on how efficient the rendering is (i.e. make it a larger number if it comes back blank).