R 我如何用2个因子填写密度表
在我的第一个面板中,我有一个绘图,根据情况通过单击或双击输入数据。如果是一次单击,则归类为射击;如果是双击,则归类为目标 同时,在另一个选项卡上,我正在创建所有这些快照的热图。然而,在我的热图(在输出$chart中的代码中生成)中,我希望在同一热图上有两种不同的颜色。一种颜色代表投篮,另一种颜色代表进球 谢谢你的帮助R 我如何用2个因子填写密度表,r,ggplot2,shiny,heatmap,R,Ggplot2,Shiny,Heatmap,在我的第一个面板中,我有一个绘图,根据情况通过单击或双击输入数据。如果是一次单击,则归类为射击;如果是双击,则归类为目标 同时,在另一个选项卡上,我正在创建所有这些快照的热图。然而,在我的热图(在输出$chart中的代码中生成)中,我希望在同一热图上有两种不同的颜色。一种颜色代表投篮,另一种颜色代表进球 谢谢你的帮助 library(shiny) library(ggplot2) ui <- fluidPage( titlePanel("Hockey"), t
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("Hockey"),
tags$img(height = 100, width = 100,
src = "Logo.png"),
sidebarPanel(
textInput(inputId = "date",
label = "Date",
value = "yyyy/mm/dd"),
textInput(inputId = "team",
label = "Team Name",
value = "Team Name"),
selectInput("shot", "shot type:",
list(`Shot Type` = list("wrist shot", "slap shot", "snap shot", "backhand", "tap in", "deflection", "one timer", "wrap around"))),
actionButton("reset", "Clear")),
mainPanel(tabsetPanel(
tabPanel("Track", plotOutput(outputId = "hockeyplot", click = "plot_click", dblclick = "plot_dblclick")),
tabPanel("Chart", plotOutput(outputId = "chart")),
server <- function(input, output){
rv <- reactiveValues(
df = data.frame(
x = numeric(),
y = numeric(),
Date = as.Date(character()),
Team = character(),
ShotType = character(),
Type = factor()
)
)
output$hockeyplot = renderPlot({
ggplot(rv$df,
aes(x = x, y = y)) + coord_flip() + lims(x = c(0, 100), y = c(42.5, -42.5)) + geom_blank + geom_point( aes(colour = factor(Type)), size = 5 ) + theme(legend.position = "none")})
observeEvent(input$plot_click, {
rv$df <- rbind(rv$df, data.frame(
x = input$plot_click$y,
y = input$plot_click$x,
Date = input$date,
Team = input$team,
ShotType = input$shot,
Type = "Shot"))
})
observeEvent(input$plot_dblclick, {
rv$df <- rbind(rv$df, data.frame(
x = input$plot_dblclick$y,
y = input$plot_dblclick$x,
Date = input$date,
Team = input$team,
ShotType = input$shot,
Type = "Goal"))
})
observeEvent(input$reset,{
rv$df <- rv$df[-nrow(rv$df),]
})
output$chart = renderPlot({
ggplot(rv$df, aes(x = x, y = y)) +
coord_flip()+
lims(x = c(0, 100), y = c(42.5, -42.5)) +
geom_blank+
theme(legend.position = "none") +
stat_density_2d(aes(fill = "shot"), geom = 'polygon', alpha = 0.4)
})
库(闪亮)
图书馆(GG2)
ui这里是一个工作示例
我创建了一个向量my_colors
来为“Shot”和“Goal”分配颜色,这样它们在图形中是一致的,并且如果Type
factor具有不同的级别数,它们不会改变
在将行添加到您的rv$df
时,我还包括了factor
。这样,当类型
的级别数发生变化时,颜色也不会发生变化。当我最初尝试运行应用程序时,在添加第二个类型后(“Shot”或“Goal”)颜色会发生变化
在stat\u 2\u density
中,您可以将fill
更改为Type
。同样,您可以指定scale\u fill\u manual
来指定相同的颜色
请让我知道这是否是你的想法
library(shiny)
library(ggplot2)
my_colours = c("Shot" = "blue", "Goal" = "green")
ui <- fluidPage(
titlePanel("Hockey"),
tags$img(height = 100, width = 100, src = "Logo.png"),
sidebarPanel(
textInput(inputId = "date",
label = "Date",
value = "yyyy/mm/dd"),
textInput(inputId = "team",
label = "Team Name",
value = "Team Name"),
selectInput("shot", "shot type:",
list(`Shot Type` = list("wrist shot", "slap shot", "snap shot", "backhand", "tap in", "deflection", "one timer", "wrap around"))),
actionButton("reset", "Clear")),
mainPanel(tabsetPanel(
tabPanel("Track", plotOutput(outputId = "hockeyplot", click = "plot_click", dblclick = "plot_dblclick")),
tabPanel("Chart", plotOutput(outputId = "chart"))
))
)
server <- function(input, output){
rv <- reactiveValues(
df = data.frame(
x = numeric(),
y = numeric(),
Date = as.Date(character()),
Team = character(),
ShotType = character(),
Type = factor(levels = c("Shot", "Goal"))
)
)
output$hockeyplot = renderPlot({
ggplot(rv$df, aes(x = x, y = y)) +
coord_flip() +
lims(x = c(0, 100), y = c(42.5, -42.5)) +
geom_blank() +
geom_point(aes(color = Type), size = 5 ) +
theme(legend.position = "none") +
scale_color_manual(values = my_colours)
})
observeEvent(input$plot_click, {
rv$df <- rbind(rv$df, data.frame(
x = input$plot_click$y,
y = input$plot_click$x,
Date = input$date,
Team = input$team,
ShotType = input$shot,
Type = factor("Shot", levels = c("Shot", "Goal"))))
})
observeEvent(input$plot_dblclick, {
rv$df <- rbind(rv$df, data.frame(
x = input$plot_dblclick$y,
y = input$plot_dblclick$x,
Date = input$date,
Team = input$team,
ShotType = input$shot,
Type = factor("Goal", levels = c("Shot", "Goal"))))
})
observeEvent(input$reset,{
rv$df <- rv$df[-nrow(rv$df),]
})
output$chart = renderPlot({
ggplot(rv$df, aes(x = x, y = y)) +
coord_flip()+
lims(x = c(0, 100), y = c(42.5, -42.5)) +
geom_blank()+
theme(legend.position = "none") +
stat_density_2d(aes(fill = Type), geom = 'polygon', alpha = .4) +
scale_fill_manual(values = my_colours)
})
}
shinyApp(ui, server)
库(闪亮)
图书馆(GG2)
my_colors=c(“射门”=“蓝色”,“进球”=“绿色”)
ui这里是一个工作示例
我创建了一个向量my_colors
来为“Shot”和“Goal”分配颜色,这样它们在图形中是一致的,并且如果Type
factor具有不同的级别数,它们不会改变
在将行添加到您的rv$df
时,我还包括了factor
。这样,当类型
的级别数发生变化时,颜色也不会发生变化。当我最初尝试运行应用程序时,在添加第二个类型后(“Shot”或“Goal”)颜色会发生变化
在stat\u 2\u density
中,您可以将fill
更改为Type
。同样,您可以指定scale\u fill\u manual
来指定相同的颜色
请让我知道这是否是你的想法
library(shiny)
library(ggplot2)
my_colours = c("Shot" = "blue", "Goal" = "green")
ui <- fluidPage(
titlePanel("Hockey"),
tags$img(height = 100, width = 100, src = "Logo.png"),
sidebarPanel(
textInput(inputId = "date",
label = "Date",
value = "yyyy/mm/dd"),
textInput(inputId = "team",
label = "Team Name",
value = "Team Name"),
selectInput("shot", "shot type:",
list(`Shot Type` = list("wrist shot", "slap shot", "snap shot", "backhand", "tap in", "deflection", "one timer", "wrap around"))),
actionButton("reset", "Clear")),
mainPanel(tabsetPanel(
tabPanel("Track", plotOutput(outputId = "hockeyplot", click = "plot_click", dblclick = "plot_dblclick")),
tabPanel("Chart", plotOutput(outputId = "chart"))
))
)
server <- function(input, output){
rv <- reactiveValues(
df = data.frame(
x = numeric(),
y = numeric(),
Date = as.Date(character()),
Team = character(),
ShotType = character(),
Type = factor(levels = c("Shot", "Goal"))
)
)
output$hockeyplot = renderPlot({
ggplot(rv$df, aes(x = x, y = y)) +
coord_flip() +
lims(x = c(0, 100), y = c(42.5, -42.5)) +
geom_blank() +
geom_point(aes(color = Type), size = 5 ) +
theme(legend.position = "none") +
scale_color_manual(values = my_colours)
})
observeEvent(input$plot_click, {
rv$df <- rbind(rv$df, data.frame(
x = input$plot_click$y,
y = input$plot_click$x,
Date = input$date,
Team = input$team,
ShotType = input$shot,
Type = factor("Shot", levels = c("Shot", "Goal"))))
})
observeEvent(input$plot_dblclick, {
rv$df <- rbind(rv$df, data.frame(
x = input$plot_dblclick$y,
y = input$plot_dblclick$x,
Date = input$date,
Team = input$team,
ShotType = input$shot,
Type = factor("Goal", levels = c("Shot", "Goal"))))
})
observeEvent(input$reset,{
rv$df <- rv$df[-nrow(rv$df),]
})
output$chart = renderPlot({
ggplot(rv$df, aes(x = x, y = y)) +
coord_flip()+
lims(x = c(0, 100), y = c(42.5, -42.5)) +
geom_blank()+
theme(legend.position = "none") +
stat_density_2d(aes(fill = Type), geom = 'polygon', alpha = .4) +
scale_fill_manual(values = my_colours)
})
}
shinyApp(ui, server)
库(闪亮)
图书馆(GG2)
my_colors=c(“射门”=“蓝色”,“进球”=“绿色”)
uigg_rink
from在哪里?这是一个单独的功能,为了简化和减少复杂性,我刚刚用geom_blank替换了gg_rink,这与geom_blank的原理相同。感谢您的帮助,gg_溜冰场是从哪里来的?它是一个独立的功能,为了简化和减少复杂性,我刚刚用geom_blank替换了gg_溜冰场,这与geom_blank的原理相同。谢谢你的帮助。本,谢谢你的帮助。谢谢你的帮助,本