2

我尝试通过 RCall 在 julia 中使用 ggplot2,如下例所示

using DataFrames
using RCall
df = DataFrame(randn(10,2),:auto)

@rlibrary ggplot2
@rlibrary ggsci
ggplot(df, aes(x=:x1, y=:x2))+geom_point()

它在 REPL 中运行良好,但在 jupyter notebook 中出现问题,如下面的调试信息所示

REvalError: Warning in (function (filename = "Rplot%03d.png", width = 480, height = 480,  :
  locale not supported by Xlib: some X ops will operate in C locale
Warning in (function (filename = "Rplot%03d.png", width = 480, height = 480,  :
  X cannot set locale modifiers
Error in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) : 
  X11 font -adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*, face 1 at size 9 could not be loaded
Calls: <Anonymous> ... grid.Call -> <Anonymous> -> descentDetails.text -> grid.Call

Stacktrace:
  [1] handle_eval_stderr(; as_warning::Bool)
    @ RCall ~/.julia/packages/RCall/iMDW2/src/io.jl:162
  [2] rprint(io::IOContext{IOBuffer}, s::Ptr{VecSxp})
    @ RCall ~/.julia/packages/RCall/iMDW2/src/io.jl:36
  [3] rprint
    @ ~/.julia/packages/RCall/iMDW2/src/io.jl:46 [inlined]
  [4] show
    @ ~/.julia/packages/RCall/iMDW2/src/io.jl:52 [inlined]
  [5] show
    @ ./multimedia.jl:47 [inlined]
  [6] limitstringmime(mime::MIME{Symbol("text/plain")}, x::RObject{VecSxp})
    @ IJulia ~/.julia/packages/IJulia/e8kqU/src/inline.jl:43
  [7] display_mimestring
    @ ~/.julia/packages/IJulia/e8kqU/src/display.jl:71 [inlined]
  [8] display_dict(x::RObject{VecSxp})
    @ IJulia ~/.julia/packages/IJulia/e8kqU/src/display.jl:102
  [9] #invokelatest#2
    @ ./essentials.jl:708 [inlined]
 [10] invokelatest
    @ ./essentials.jl:706 [inlined]
 [11] execute_request(socket::ZMQ.Socket, msg::IJulia.Msg)
    @ IJulia ~/.julia/packages/IJulia/e8kqU/src/execute_request.jl:112
 [12] #invokelatest#2
    @ ./essentials.jl:708 [inlined]
 [13] invokelatest
    @ ./essentials.jl:706 [inlined]
 [14] eventloop(socket::ZMQ.Socket)
    @ IJulia ~/.julia/packages/IJulia/e8kqU/src/eventloop.jl:8
 [15] (::IJulia.var"#15#18")()
    @ IJulia ./task.jl:411

我尝试将图片保存为 pdf、png 或 svg 格式,但只有 pdf 可以正常工作:png 和 svg 会引发错误

g = ggplot(df, aes(x=:x1, y=:x2))+geom_point()
ggsave(joinpath(odir,"try.png"),g)
REvalError: Saving 7 x 7 in image
Warning in png_dev(..., res = dpi, units = "in") :
  locale not supported by Xlib: some X ops will operate in C locale
Warning in png_dev(..., res = dpi, units = "in") :
  X cannot set locale modifiers
Error in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) : 
  X11 font -adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*, face 1 at size 9 could not be loaded
Calls: <Anonymous> ... grid.Call -> <Anonymous> -> descentDetails.text -> grid.Call

Sup
Run R 中的相应代码也可以。

> library(ggplot2)
> df = data.frame(x1=rnorm(10),x2=rnorm(10))
> df
            x1          x2
1   0.18165143  0.38032665
2  -0.62212533  0.52412366
3   0.98072899  0.30052532
4  -0.76721485 -0.63748290
5   0.00894366  0.04508702
6  -0.33166441  0.37056327
7  -0.14156795 -1.06334665
8  -1.08918837 -0.30631348
9   0.22035254  0.78816722
10  0.00356827 -0.62542425
> ggplot(df, aes(x=x1,y=x2))+geom_point()

它在这里生成图片: 由ggplot生成的情节


环境
R 会话

R version 4.1.1 (2021-08-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /mnt/Storage/home/yangdongxu/softwares/R/R_4.1_installed/lib/R/lib/libRblas.so
LAPACK: /mnt/Storage/home/yangdongxu/softwares/R/R_4.1_installed/lib/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] ggplot2_3.3.5

loaded via a namespace (and not attached):
 [1] magrittr_2.0.1   tidyselect_1.1.1 munsell_0.5.0    colorspace_2.0-2
 [5] R6_2.5.1         rlang_0.4.12     fansi_0.5.0      dplyr_1.0.7
 [9] grid_4.1.1       gtable_0.3.0     utf8_1.2.2       DBI_1.1.1
[13] withr_2.4.3      ellipsis_0.3.2   assertthat_0.2.1 digest_0.6.29
[17] tibble_3.1.6     lifecycle_1.0.1  crayon_1.4.2     purrr_0.3.4
[21] farver_2.1.0     vctrs_0.3.8      glue_1.5.1       labeling_0.4.2
[25] compiler_4.1.1   pillar_1.6.4     generics_0.1.1   scales_1.1.1
[29] pkgconfig_2.0.3

朱莉娅:版本 1.6.4


那么我应该怎么做才能像python %matplotlib inline一样直接在jupyter中显示图片呢?

谢谢。

4

0 回答 0