R 根据用户输入高亮显示ggplot区域
我在ggplot中创建了一个篮球半场,我将它嵌入了Shiny中的一个侧栏面板中,我想根据用户输入突出显示球场的“区域”。我在想,我可以使用renderUI和geom_rect()相结合的方法来获得我想要的东西,但我尝试过的方法似乎都不管用。有人能帮忙吗 我附加了一个图片链接,希望能帮助我补充上面的解释和当前代码 谢谢大家!R 根据用户输入高亮显示ggplot区域,r,dynamic,ggplot2,shiny,spatial,R,Dynamic,Ggplot2,Shiny,Spatial,我在ggplot中创建了一个篮球半场,我将它嵌入了Shiny中的一个侧栏面板中,我想根据用户输入突出显示球场的“区域”。我在想,我可以使用renderUI和geom_rect()相结合的方法来获得我想要的东西,但我尝试过的方法似乎都不管用。有人能帮忙吗 我附加了一个图片链接,希望能帮助我补充上面的解释和当前代码 谢谢大家! teams使用基本绘图,您可以有一个透明绘图(renderPlot({…},bg=“transparent”)),添加透明矩形(rect(…,col=rgb(0,50,25
teams使用基本绘图,您可以有一个透明绘图(renderPlot({…},bg=“transparent”)
),添加透明矩形(rect(…,col=rgb(0,50,255,50,maxColorValue=256))
),并通过CSS(HTML(#plot{background:url)将图片添加为背景(https://...)}))
)
有关示例应用程序,请参见以下内容:
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)
bckpic请分享teams
以便代码可复制,@BigDataScientist-谢谢你的帮助!我添加了teams
以实现可复制性。你下面的代码就可以了!@BigDataScientist-有没有办法让正方形一直延伸到图像的角落?别提问题above.通过简单地更改plot()
中的ylim
和xlim
数字就可以解决这个问题。再次感谢您提供了非常简单的答案,它大大简化了我的整个代码。
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)