我不是百分之百确定如何恰当地提出这个问题,但我希望解决的问题应该是相当简单的。
我感兴趣的是在R中制作交互式散点图,它不是基于为x轴或y轴选择新值,而是从数据集中的另一个变量进行更改。
具体地说,我希望从1998年至2019年(x轴)为给定团队的收入(y轴),并希望能够改变哪个团队正在显示。目前,每个团队都是同时显示的,这使得图表很难理解。我不希望x轴或y轴发生变化,只希望每次显示的数据点对应于单个团队。
我使用的代码,结果是一个非交互式的图形,同时显示每个数据点,如下所示:
ui <- fluidPage(
titlePanel("Concede-and-Divide Rule Allocation 1998-2019"),
sidebarLayout(
sidebarPanel(
#option for selecting teams
selectInput(
"HmTm", "Home Team:",
choices=same_team$HmTm
),
),
mainPanel(
plotOutput(outputId = "scatterplot")
)))
server <- function(input, output) {
output$scatterplot <- renderPlot({
ggplot(same_team, aes(x = year, y = Concede_and_Divide_Revenue, color = HmTm)) +
geom_point() + xlab("Year") + ylab("Concede-and-Divide Allocation") +
ggtitle("Concede-and-Divide Rule Allocation 1998-2019") +
theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1))
})
}
shinyApp(ui = ui, server = server)发布于 2021-04-11 03:10:43
您只需要将正在绘制的数据框设为子集。尝尝这个
library(gapminder)
same_team <- gapminder[gapminder$continent=="Americas",]
same_team$HmTm <- same_team$country
same_team$Concede_and_Divide_Revenue <- same_team$gdpPercap
ui <- fluidPage(
titlePanel("Concede-and-Divide Rule Allocation 1998-2019"),
sidebarLayout(
sidebarPanel(
#option for selecting teams
selectInput(
"HmTm", "Home Team:",
choices= unique(same_team$HmTm)
),
),
mainPanel(
plotOutput(outputId = "scatterplot")
)))
server <- function(input, output) {
output$scatterplot <- renderPlot({
ggplot(same_team[same_team$HmTm==input$HmTm,], aes(x = year, y = Concede_and_Divide_Revenue, color = HmTm)) +
geom_point() + xlab("Year") + ylab("Concede-and-Divide Allocation") +
ggtitle("Concede-and-Divide Rule Allocation 1998-2019") +
theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1))
})
}
shinyApp(ui = ui, server = server)

https://stackoverflow.com/questions/67036453
复制相似问题