如何在交互式RMarkdown中控制绘图高度/大小(使用Shiny) [英] How to control plot height/size in interactive RMarkdown (with Shiny)

查看:790
本文介绍了如何在交互式RMarkdown中控制绘图高度/大小(使用Shiny)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处所述,我正在将Shiny与RMarkdown一起用于生成交互式文档. /p>

使用以下代码,我设法绘制了交互式地图

```{r, echo=FALSE, warning=FALSE, message=FALSE}

g2g14 <- readOGR("input//geodata", "g2g14") # no projection needs to be specified
old_geodata <- g2g14@data

inputPanel(
  selectInput("map.party", label = "Partei", choices = unique(long_data$Partei), selected = "FDP"),
  selectInput("map.year", label = "Wahljahr", choices = unique(format.Date(long_data$Jahr, "%Y")), selected = "1971")
)

renderPlot({
  partydata <- long_data %>%
    filter(Partei == input$map.party & Jahr == as.Date(input$map.year, "%Y"))
  g2g14@data <- old_geodata
  g2g14@data <- merge(g2g14@data, partydata, by.x =  "GMDNR",by.y ="BFSNr")

  cols <- brewer.pal(5, "Purples")
  brks <- seq(0,100,20)
  colsForMap <- cols[findInterval(g2g14@data$Staerke, vec = brks[1:5])]

  plot(g2g14, col = colsForMap, border = "white")
  legend("topleft", legend = levels(cut(g2g14@data$Staerke, brks)), fill = cols, border = "white", title = "Parteistärke in %")

})

问题:从控制台运行代码时,地图可以很好地缩放,但是在交互式文档中,图太小了:

这可能是由于绘图区域高度有限所致. 我已经尝试在块选项中设置一个很大的fig.height = 20,但是没有结果. 该怎么办?

解决方案

您只需在renderPlot中添加widthheight选项.键入?renderPlot以获得更多信息.

就您而言,

renderPlot({
  partydata <- long_data %>%
    filter(Partei == input$map.party & Jahr == as.Date(input$map.year, "%Y"))
  g2g14@data <- old_geodata
  g2g14@data <- merge(g2g14@data, partydata, by.x =  "GMDNR",by.y ="BFSNr")

  cols <- brewer.pal(5, "Purples")
  brks <- seq(0,100,20)
  colsForMap <- cols[findInterval(g2g14@data$Staerke, vec = brks[1:5])]

  plot(g2g14, col = colsForMap, border = "white")
  legend("topleft", legend = levels(cut(g2g14@data$Staerke, brks)), fill = cols, border = "white", title = "Parteistärke in %")
}, width = 1200, height = 900)  ## <--- add outside the curly braces

I am using Shiny together with RMarkdown to produce an interactive document, as described here.

Using the following code, I managed to plot an interactive map

```{r, echo=FALSE, warning=FALSE, message=FALSE}

g2g14 <- readOGR("input//geodata", "g2g14") # no projection needs to be specified
old_geodata <- g2g14@data

inputPanel(
  selectInput("map.party", label = "Partei", choices = unique(long_data$Partei), selected = "FDP"),
  selectInput("map.year", label = "Wahljahr", choices = unique(format.Date(long_data$Jahr, "%Y")), selected = "1971")
)

renderPlot({
  partydata <- long_data %>%
    filter(Partei == input$map.party & Jahr == as.Date(input$map.year, "%Y"))
  g2g14@data <- old_geodata
  g2g14@data <- merge(g2g14@data, partydata, by.x =  "GMDNR",by.y ="BFSNr")

  cols <- brewer.pal(5, "Purples")
  brks <- seq(0,100,20)
  colsForMap <- cols[findInterval(g2g14@data$Staerke, vec = brks[1:5])]

  plot(g2g14, col = colsForMap, border = "white")
  legend("topleft", legend = levels(cut(g2g14@data$Staerke, brks)), fill = cols, border = "white", title = "Parteistärke in %")

})

The problem: The map is nicely scaled when running the code from the console, but in the interactive document, the plot is too small:

This probably results from the limited height of the plotting region. I've already tried setting a really large fig.height = 20 in the chunk options, but with no outcome. What to do?

解决方案

You can just add width and height options in renderPlot. Type ?renderPlot for more information.

In your case,

renderPlot({
  partydata <- long_data %>%
    filter(Partei == input$map.party & Jahr == as.Date(input$map.year, "%Y"))
  g2g14@data <- old_geodata
  g2g14@data <- merge(g2g14@data, partydata, by.x =  "GMDNR",by.y ="BFSNr")

  cols <- brewer.pal(5, "Purples")
  brks <- seq(0,100,20)
  colsForMap <- cols[findInterval(g2g14@data$Staerke, vec = brks[1:5])]

  plot(g2g14, col = colsForMap, border = "white")
  legend("topleft", legend = levels(cut(g2g14@data$Staerke, brks)), fill = cols, border = "white", title = "Parteistärke in %")
}, width = 1200, height = 900)  ## <--- add outside the curly braces

这篇关于如何在交互式RMarkdown中控制绘图高度/大小(使用Shiny)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆