首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在R Studio演示文稿中包含情节(Rpres)

如何在R Studio演示文稿中包含情节(Rpres)
EN

Stack Overflow用户
提问于 2016-08-19 17:14:23
回答 2查看 5.2K关注 0票数 14

如何在Rpres文件中包含绘图?如果你像在一个普通的Rmd文件中那样做

代码语言:javascript
复制
Basic Plot
========================================================
```{r, echo=FALSE}

库(绘图)

Plot_ly(经济,x=日期,y=失业/流行)

代码语言:javascript
复制

结果如下所示:

我想出的解决方案是使用Markdown可以包含HTML的可能性:

代码语言:javascript
复制
Basic Plot
========================================================
```{r, results='hide', echo=FALSE}

库(绘图)

P=plot_ly(经济,x=日期,y=失业/流行)

As.widget::saveWidget(demo.html(P),file =“htmlwidgets”)

代码语言:javascript
复制
<iframe src="demo.html" style="position:absolute;height:100%;width:100%"></iframe>

但我希望有一个更优雅的解决方案,不使用任何额外的文件。

EN

回答 2

Stack Overflow用户

发布于 2016-12-08 20:33:27

下面是一个关于如何在ioslides演示文稿中包含plot_ly图的最小示例,因此它不能很好地回答Rpres的问题,但提供了一种替代方案。

第一张幻灯片显示了从ggplot转换为plot_ly的图,保留了ggplot样式。第二张幻灯片显示了一个直接使用plot_ly的绘图。

代码语言:javascript
复制
---
title: "Plot_ly demo"
date: "8 December 2016"
output: ioslides_presentation
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo =假)

代码语言:javascript
复制
## 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)

代码语言:javascript
复制
## Another simple plot_ly

```{r, echo = FALSE, fig.align = 'center', message = FALSE}

plot_ly(df,x= x,y= y)

代码语言:javascript
复制
票数 5
EN

Stack Overflow用户

发布于 2018-11-22 20:32:57

也有同样的问题。当我执行slidify(index.Rmd)时,有一条消息说是PhantomJS not found,并建议我运行webshot::install_phantomjs()。所以我这样做了,错误就消失了。然而,我仍然没有得到地图式交互式地图输出。它是空白的。

我还在终端中尝试了以下代码,它对一些人有效,但对我不起作用。我得到了html文件的输出,但仍然没有地图。它来自这个post。这可能对你有用。

代码语言:javascript
复制
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

代码语言:javascript
复制
htmlwidgets::saveWidget(as_widget(p), "p.html")
cat('<iframe src="./p.html" width=100% height=100% allowtransparency="true"> </iframe>')

完整的上下文将在下面列出。

代码语言:javascript
复制
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>')
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39035308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档