【发布时间】:2018-02-01 17:46:07
【问题描述】:
我正在 ggplot2 中生成一个简单的条形图,并且希望在 selectizeInput() 控件中选择相关值时使用闪亮(通过 R Markdown)在条形上方添加 png 图像。
基本上,在这个例子中有一个数据框,它有 RD、OPP 和 FP,x 轴是 RD,y 轴是 FP。然后我可以使用选择工具选择 5 个 OPP 中的任意数量。
选择这些OPP中的任何一个时,我希望相应的PNG图像位于图表中相应栏的顶部(或附近)。请注意,已加载的 png 图像与 OPP 中的术语名称相同。
我已尝试根据以下链接中的建议使用 annotation_raster 公式来执行此操作,但由于我是动态的,因此它并不完全相同,实际上往往会扰乱图表。 Create a Scatterplot of Raster Images in R
如果有人能够提供帮助,将不胜感激。为了完整起见,指向 png 文件位置的链接是 here
---
title: "Raster Question for Stack Overflow"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(png)
library(grid)
library(ggplot2)
stats<-data.frame(RD=c(1:5),OPP=c("ADEL","BL","ESS","SYD","HAW"),FP=c(40,45,60,30,50))
ADEL <- rasterGrob(readPNG("ADEL.png"), interpolate=TRUE)
BL <- rasterGrob(readPNG("BL.png"), interpolate=TRUE)
ESS <- rasterGrob(readPNG("ESS.png"), interpolate=TRUE)
SYD <- rasterGrob(readPNG("SYD.png"), interpolate=TRUE)
HAW <- rasterGrob(readPNG("HAW.png"), interpolate=TRUE)
```
Column
-----------------------------------------------------------------------
### Plot
```{r, echo = FALSE}
selectizeInput("opponent", label = "Select Opponent:",
choices = stats$OPP, multiple = TRUE)
renderPlot({
ggplot(stats,aes(RD,FP))+
theme_bw()+
theme(strip.background = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_blank(),
axis.ticks = element_line(size = 0),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank())+
geom_bar(position="dodge",stat="identity",fill = "deepskyblue")+
mapply(function(xx, yy, i)
annotation_raster(as.character(stats$OPP[stats$OPP %in% input$opponent])[[i]],
xmin=xx-0.5, xmax=xx+0.5, ymin=yy+5, ymax=yy+10),
stats$RD[stats$OPP %in% input$opponent],
stats$FP[stats$OPP %in% input$opponent],
stats$OPP[stats$OPP %in% input$opponent])
})
```
【问题讨论】:
标签: r ggplot2 shiny flexdashboard