R base package grid does not produce output

后端 未结 2 527
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-15 06:13

I am running 64-bit R 2.15.0 on a Windows Server 2008 R2 Amazon EC2 instance. grid does not produce output. For example, the following code should produce a sin

相关标签:
2条回答
  • 2021-01-15 06:35

    R does not produce raster images correctly in the window device over Remote Desktop Connection. If a raster image is required, the plot must be output to another device.

    library(ggplot2)
    library(grid)
    library(maps)
    library(mapproj)
    library(png)
    library(RgoogleMaps)
    
    counties <- map_data("county", region="virginia")
    states <- map_data("state")
    
    tmp <- tempfile(fileext=".png")
    bg <- GetMap.bbox(range(counties$long), range(counties$lat), destfile=tmp, 
         maptype="satellite", format="png32")
    background <- readPNG(tmp)
    background <- rasterGrob(unclass(background))
    
    p <- ggplot(counties, aes(long, lat)) +
       coord_map(xlim=c(bg$BBOX$ll[2], bg$BBOX$ur[2]), 
                 ylim=c(bg$BBOX$ll[1], bg$BBOX$ur[1])) +
       geom_path(aes(group=group), color="darkgrey") +
       geom_path(data=states, aes(group=group), color="white", size=1) +
       opts(axis.line=theme_blank(),
            axis.text.x=theme_blank(),
            axis.text.y=theme_blank(),
            axis.ticks=theme_blank(),
            axis.title.x=theme_blank(),
            axis.title.y=theme_blank(),
            axis.ticks.length=unit(0, "lines"),
            axis.ticks.margin=unit(0, "lines"),
            panel.border=theme_blank(),
            panel.background=function(...)background,
            panel.grid.major=theme_blank(),
            panel.grid.minor=theme_blank(),
            panel.margin=unit(0, "lines"),
            legend.position="none",
            legend.title=theme_blank(),
            legend.background=theme_blank(),
            plot.margin=unit(0*c(-1.5, -1.5, -1.5, -1.5), "lines"))
    
    pdf("plot.pdf", height=7, width=7)
    p
    dev.off()
    

    I have found that writing plotting commands between the pdf() and dev.off() produces blank files. Storing the plot in an object and calling it will work.

    0 讨论(0)
  • 2021-01-15 06:43

    dev.list() can be called to return a named vector of open graphics devices. On Windows, for example:

    windows()
    pdf()
    dev.list()
    # windows     pdf 
    #       2       3 
    dev.off(); dev.off()
    dev.list()
    # NULL
    

    And dev.cur() will return the currently active device. If there are no devices open, you can open one:

    windows()
    grid.newpage()
    l <- linesGrob()
    grid.draw(l)
    

    For pdf, you have to be sure to close the device or else the pdf file will not render:

    pdf() # plot saved by default to Rplots.pdf
    grid.newpage()
    l <- linesGrob()
    grid.draw(l)
    dev.off() 
    

    The ?device help page lists the other graphics devices. Usually a call to grid.newpage() automatically opens a new device if none are open, but perhaps not in your case. The above examples work for me on Windows 7 x64 and Ubuntu 11.10 x64.

    @attitude_stool: Does any of the above help identify your problem?

    0 讨论(0)
提交回复
热议问题