# 在R用plot_ly画了一张饼图
pie_all=plot_ly(labels = tbl_all$`Cell type`, values = ~Percent, showlegend = F) %>%
add_pie(data=tbl_all,name="Overall Proportion",title="Overall Proportion")
1. plot_ly画图对象无法直接通过pdf(), dev.off()保存,这样保存的图片无法打开
2. 可以通过export()函数倒数,但是这种保存方式无法指定图片的一些参数,比如长度宽度等。
export(p = last_plot(), file = "plotly.png", selenium = NULL, ...)
3. 通过save_image函数保存图像,但是我在运行save_image函数时花样报错,解决方法如下
# 可以指定图片长度宽度
save_image(p, file, ..., width = NULL, height = NULL, scale = NULL)
plotly是基于python开发的package,因此,在R中无论是调用plot_ly还是保存图片,均需要涉及到python。首先需要明确R是否能够通过reticulate包调用python和miniconda
# 在运行安装之前最好先清理内存或重启R Studio,不然可能报错
# 确保电脑已经安装好python或anaconda
# 安装reticulate
install.packages("reticulate")
library(reticulate)
# 检查python是否可用
py_available()
[1] FALSE
# 指定调用python路径
Sys.setenv(RETICULATE_PYTHON="F:\\Anaconda3\\python.exe")
use_python("F:\\Anaconda3\\python.exe")
# Retrieve information about the version of Python currently being used by reticulate.
py_config()
# 成功指定路径
py_available()
[1] TRUE
save_image函数需要调用python的kaleido包,继续安装
# 方式一,报错多
# save_image函数帮助文档有一种推荐的安装方式,反正我一运行就报错
?save_image
install.packages('reticulate')
reticulate::install_miniconda()
reticulate::conda_install('r-reticulate', 'python-kaleido')
reticulate::conda_install('r-reticulate', 'plotly', channel = 'plotly')
reticulate::use_miniconda('r-reticulate')
# 方式二
py_install("python-kaleido")
py_install("r-reticulate")
py_install("plotly")