如何在Rpres文件中包含绘图?如果你像在一个普通的Rmd文件中那样做
Basic Plot
========================================================
```{r, echo=FALSE}库(绘图)
Plot_ly(经济,x=日期,y=失业/流行)
结果如下所示:

我想出的解决方案是使用Markdown可以包含HTML的可能性:
Basic Plot
========================================================
```{r, results='hide', echo=FALSE}库(绘图)
P=plot_ly(经济,x=日期,y=失业/流行)
As.widget::saveWidget(demo.html(P),file =“htmlwidgets”)
<iframe src="demo.html" style="position:absolute;height:100%;width:100%"></iframe>但我希望有一个更优雅的解决方案,不使用任何额外的文件。
发布于 2016-12-08 20:33:27
下面是一个关于如何在ioslides演示文稿中包含plot_ly图的最小示例,因此它不能很好地回答Rpres的问题,但提供了一种替代方案。
第一张幻灯片显示了从ggplot转换为plot_ly的图,保留了ggplot样式。第二张幻灯片显示了一个直接使用plot_ly的绘图。
---
title: "Plot_ly demo"
date: "8 December 2016"
output: ioslides_presentation
---
```{r setup, include=FALSE}knitr::opts_chunk$set(echo =假)
## A simple plot_ly
```{r, fig.align='center', message = FALSE}库(绘图)
df <- data.frame(x = 1:10,y= (1:10)^2)
P <- ggplot(df,aes(x = x,y= y)) + geom_line() + labs(x = "X",y= "Y",title = "X and Y")
ggplotly(p)
## Another simple plot_ly
```{r, echo = FALSE, fig.align = 'center', message = FALSE}plot_ly(df,x= x,y= y)
发布于 2018-11-22 20:32:57
也有同样的问题。当我执行slidify(index.Rmd)时,有一条消息说是PhantomJS not found,并建议我运行webshot::install_phantomjs()。所以我这样做了,错误就消失了。然而,我仍然没有得到地图式交互式地图输出。它是空白的。
我还在终端中尝试了以下代码,它对一些人有效,但对我不起作用。我得到了html文件的输出,但仍然没有地图。它来自这个post。这可能对你有用。
Rscript -e "library(knitr); library(rmarkdown);
rmarkdown::render('index.Rmd', output_file='index.html')"我相信这是有预谋的。因为ggplots运行得很好。
更新:
通过运行install.packages("webshot"),然后再次运行webshot::install_phantomjs(),然后运行library(knitr); library(rmarkdown); rmarkdown::render('index.Rmd', output_file='index.html'),重新安装/更新了wetshot包。啊,真灵。html文件有一个绘图地图,尽管它不会出现在Knitr预览窗口中。
更新:
通过添加以下代码,我可以在侧面显示地图。请参阅此post。
htmlwidgets::saveWidget(as_widget(p), "p.html")
cat('<iframe src="./p.html" width=100% height=100% allowtransparency="true"> </iframe>')完整的上下文将在下面列出。
library(plotly)
cities <- readRDS("D:/R/data/cn_cities.rds")
cities <- cities[1:50,]
geo <- list(
scope = 'asia',
projection = list(type = 'Mercator'),
showland = TRUE,
landcolor = toRGB("gray85"),
countrycolor = toRGB("white"),
subunitcolor = toRGB("white"),
countrywidth = 1,
subunitwidth = 1)
p <- plot_geo(cities,
locationmode='CHN',
sizes=c(1, 200)) %>%
add_markers(x=~lng, y=~lat,
size=~sqrt(population),
hoverinfo="text",
text=~paste(city, "<br />", population)) %>%
layout(title='',
geo=geo)
htmlwidgets::saveWidget(as_widget(p), "p.html")
cat('<iframe src="./p.html" width=100% height=100% allowtransparency="true"> </iframe>')https://stackoverflow.com/questions/39035308
复制相似问题