如何在交互式 RMarkdown 中控制绘图 height/size(使用 Shiny)
How to control plot height/size in interactive RMarkdown (with Shiny)
我正在使用 Shiny 和 RMarkdown 来制作交互式文档,如 here 所述。
使用下面的代码,我设法绘制了一个交互式地图
```{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
,但没有结果。
该怎么办?
您只需一个额外步骤即可完成此操作 - 添加 renderUI
:
```{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")
)
output$unsized <- 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 %")
})
renderUI({
plotOutput("unsized", height = 500, width = 500)
})
```
使用启动新 RMD-shiny 文档时创建的示例文档进行测试:
```{r, echo=FALSE}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
output$unsized <- renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
renderUI({
plotOutput("unsized", height = 250, width = 250)
})
```
您可以在 renderPlot
中添加 width
和 height
选项。键入 ?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
我正在使用 Shiny 和 RMarkdown 来制作交互式文档,如 here 所述。
使用下面的代码,我设法绘制了一个交互式地图
```{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
,但没有结果。
该怎么办?
您只需一个额外步骤即可完成此操作 - 添加 renderUI
:
```{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")
)
output$unsized <- 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 %")
})
renderUI({
plotOutput("unsized", height = 500, width = 500)
})
```
使用启动新 RMD-shiny 文档时创建的示例文档进行测试:
```{r, echo=FALSE}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
output$unsized <- renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
renderUI({
plotOutput("unsized", height = 250, width = 250)
})
```
您可以在 renderPlot
中添加 width
和 height
选项。键入 ?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