根据用户输入闪亮突出显示ggplot的区域

时间:2017-06-09 18:53:56

标签: r dynamic ggplot2 shiny spatial

我在ggplot中创建了一个篮球半场,我已经嵌入了Shiny的侧边栏内,我想根据用户输入突出显示球场的“区域”。我以为我可以使用renderUI和geom_rect()这样的东西来获得我想要的东西,但我尝试过的东西似乎都没有用。任何人都可以帮忙吗?

我附上了一个图片链接,希望有助于补充我上面的解释以及当前的代码。

谢谢!

Court Zones Example

((RecipeItemListener)getActivity()).doSomethingOnListener()

1 个答案:

答案 0 :(得分:1)

使用基础图可以使用透明图(renderPlot({...}, bg="transparent")),向其添加透明矩形(rect(..., col = rgb(0, 50, 255, 50, maxColorValue = 256)))并通过CSS(HTML("#plot{background:url(https://...)})))将图片添加为背景。< / p>

enter image description here

有关示例应用,请参阅以下内容:

bckpic <- "https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg"

pos <- function(x, y){
  xx <- x1 <- (x - 1)*5 + c(0, 5)
  yy <- 25 - ((y - 1)*5 + c(0, 5))
  return(c(xx[1], yy[2], xx[2], yy[1]))
}

ui <- fluidPage(
  tags$style(type='text/css', HTML("#plot{background:url(https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg);
                                    background-size: 200px 200px;
                                    background-repeat: no-repeat;}")),
  selectInput("pass", "Pass Location", 1:25),
  selectInput("possess", "Possession Location", 1:25, 25),
  uiOutput("style"),
  plotOutput("plot")
)


server <- function(input, output){
  output$plot <- renderPlot({
    par(mar = c(0,0,0,0))
    plot(0, 0, ylim = c(0,25), xlim = c(0, 25), type='p', yaxt = "n", 
         xaxt = "n", xlab = "", ylab = "")
    nr <- as.numeric(input$pass)
    posi <- pos(ifelse(nr%%5 > 0, nr%%5, 5),ceiling(nr/5))
    rect(posi[1], posi[2], posi[3], posi[4], col = rgb(0, 50, 255, 50, maxColorValue = 256))

    nr <- as.numeric(input$possess)
    posi <- pos(ifelse(nr%%5 > 0, nr%%5, 5),ceiling(nr/5))
    rect(posi[1], posi[2], posi[3], posi[4], col = rgb(255, 50, 0, 50, maxColorValue = 256))

  }, bg="transparent", width = 200, height = 200)
}

runApp(shinyApp(ui, server), launch.browser = TRUE)