我有一个ggplot中创建的篮球半场,里面嵌入了Shiny中的sidebarPanel,我想根据用户输入突出显示“区域”。我想我可以使用renderUI和geom_rect()这两行来获得我想要的东西,但是我尝试过的东西似乎没有用。有人能帮忙吗?基于用户输入闪亮的ggplot突出显示的区域
我附上了一个图像链接,希望有助于补充我的解释以及当前的代码。
谢谢!
teams <- c("Hawks","Celtics","Nets","Hornets","Bulls","Cavaliers",
"Mavericks","Nuggets","Pistons","Warriors","Rockets","Pacers",
"Clippers","Lakers","Grizzlies","Heat","Bucks","Timberwolves",
"Pelicans","Knicks","Thunder","Magic","76ers","Suns","Trail
Blazers", "Kings","Spurs","Raptors","Jazz","Wizards")
server <- function(input, output) {
output$half_court <- renderPlot({
ggplot() + geom_polygon(data = court[court$side==1,], aes(x = x, y = y,
group = group), col = "gray") +
coord_equal() +
xlim(-2,50) +
ylim(-2,50) +
scale_x_continuous(breaks = c(0, 25, 50)) +
scale_y_continuous(breaks = c(0, 12.5, 25, 37.5, 50)) +
xlab("") + ylab("") +
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(), axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(), axis.title = element_blank()) +
theme(panel.background = element_rect(fill = 'white')) +
geom_rect(aes_string(xmin = 0, xmax = 10, ymin = 37.6, ymax = 47), fill
= "yellow", alpha = 0.20) +
geom_rect(aes_string(xmin = 40, xmax = 50, ymin = 0, ymax = 9.4), fill
= "green", alpha = 0.20)
}, bg = "transparent")
}
ui <- fluidPage(
titlePanel(title=div(img(src="primary.png", height = 50, width =
50),strong("Database"))),
sidebarLayout(
sidebarPanel(
selectInput("season", "Season",c("","2016","2015","2014")),
selectInput("team", "Team 1",c("",teams)),
selectInput("team", "Team 2",c("",teams)),
selectInput("pass", "Pass Location",c("",1:25)),
selectInput("poss", "Possession Location",c("",1:25)),
plotOutput(outputId = "half_court")
),
mainPanel()
)
)
shinyApp(ui = ui, server = server)
请分享'teams',这样的代码是可重复的,... – BigDataScientist
@BigDataScientist - 谢谢您的帮助!我已经添加了'团队'来进行重复性。你的代码在这里诀窍! – Abb
@BigDataScientist - 有没有办法让广场一直延伸到图像的角落? – Abb