• linux命令行下使用R语言绘图


    系统:centos 6.4 64bit

    环境安装参考:http://hi.baidu.com/solohac/item/4a18e78f1bef9b5825ebd99c

    在R语言中可以使用png()等函数生成图片,例如: png("aa.png")可以生成图片。

    但是如果你是通过shell远程连接到系统上,可能会碰到如下错误:

    1
    2
    3
    4
    5
    > png("aa.png")
    错误于.External2(C_X11, paste("png::", filename, sep = ""), g$width,  :
      无法打开PNG设备
    此外: 警告信息:
    In png("aa.png") : 无法打开链结到X11显示''

    分析:

    打开R控制台,输入capabilities(),可以看到:

    1
    2
    3
    4
    5
    > capabilities()
        jpeg      png     tiff    tcltk      X11     aqua http/ftp  sockets
       FALSE    FALSE    FALSE    FALSE    FALSE    FALSE     TRUE     TRUE
      libxml     fifo   cledit    iconv      NLS  profmem    cairo
        TRUE     TRUE     TRUE     TRUE     TRUE    FALSE    FALSE

    可以看到png为false,因为生成图片需要X11,但是shell是没有X11的。

    怎么解决:

    1.在X11模式下执行R语言命令,也就是在linux的桌面。如果你只是在桌面上画画图,那选择这个方式就可以了。

    2.不使用X11生成,使用 图形渲染库Cairo。比如我,需要用脚本,根据实时数据生成统计图,那么用这个方式就很方便。

    安装:

    启动R控制台

    R

    安装

    1
    install.packages("Cairo")

    会让你选择安装镜像

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    --- 在此連線階段时请选用CRAN的鏡子 ---
    CRAN mirror
     
     1: 0-Cloud                       2: Argentina (La Plata)      
     3: Argentina (Mendoza)           4: Australia (Canberra)      
     5: Australia (Melbourne)         6: Austria                   
     7: Belgium                       8: Brazil (BA)               
     9: Brazil (PR)                  10: Brazil (RJ)               
    11: Brazil (SP 1)                12: Brazil (SP 2)             
    13: Canada (BC)                  14: Canada (NS)               
    15: Canada (ON)                  16: Canada (QC 1)             
    17: Canada (QC 2)                18: Chile                     
    19: China (Beijing 1)            20: China (Beijing 2
    选一个北京的好了:20

    安装出错

    configure: error: Cannot find cairo.h! Please install cairo (http://www.cairographics.org/) and/or set CAIRO_CFLAGS/LIBS correspondingly.

    所以我们需要先在系统中安装cairo库

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    yum -y install cairo* libxt*
    安装完成之后,重新执行:install.packages("Cairo")
    installing to /usr/local/lib64/R/library/Cairo/libs
    ** R
    ** preparing package for lazy loading
    ** help
    *** installing help indices
    ** building package indices
    ** testing if installed package can be loaded
    * DONE (Cairo)
     
    下载的程序包在
    ‘/tmp/RtmpNlGyf3/downloaded_packages’里
    更新'.Library'里的HTML程序包列表
    Making 'packages.html' ... 做完了。

    安装成功。

    加载Cairo后,查看支持:

    1
    2
    3
    4
    5
    6
    7
    > library(Cairo)
    > Cairo.capabilities()
       png   jpeg   tiff    pdf    svg     ps    x11    win raster
      TRUE  FALSE  FALSE   TRUE   TRUE   TRUE   TRUE  FALSE   TRUE
    说明已经支持用cairo生成图片了。
    例如可以使用如下命令生成PNG:
    CairoPNG(file="out.png",width=800,height=480)

    附上一个测试小程序,主要为了说明用法:

    #vim test.r

    library(Cairo)

    CairoPNG(file="out.png",width=800,height=480)

    plot(5,4)

    保存后执行:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@localhost ~]# Rscript test.r
     
    R version 3.1.0 (2014-04-10) -- "Spring Dance"
    Copyright (C) 2014 The R Foundation for Statistical Computing
    Platform: x86_64-unknown-linux-gnu (64-bit)
    > library(Cairo)
    > CairoPNG(file="out.png",width=800,height=480)
    > plot(5,4)
    >

    在当前目录下可以看到生成了图片

    1
    2
    3
    4
    [root@localhost ~]# ll
    总用量 84
    -rw-r--r--. 1 root root  6832 5月  13 10:33 out.png
    -rw-r--r--. 1 root root    71 5月  13 10:32 test.r
  • 相关阅读:
    Hrbust-1492 盒子(二分图最大匹配)
    数据结构——二叉树的建立和遍历(递归建树&层序遍历建树)
    HDU 1710 二叉树遍历
    HDU 2891
    HDU 2895 贪心 还是 大水题
    POJ 2896 另解暴力
    POJ 2896 AC自动机 or 暴力
    HDU 1714 math
    POJ 1328 贪心
    POJ 2109 巧妙解法
  • 原文地址:https://www.cnblogs.com/solohac/p/4154148.html
Copyright © 2020-2023  润新知