R 与非';t在x或y轴上
我不是100%确定如何正确地问这个问题,但我希望解决的问题应该非常简单 我感兴趣的是在R中创建一个交互式散点图,它的变化不是基于为x轴或y轴选择一个新值,而是来自数据集中的另一个变量 具体而言,我希望为给定团队绘制1998-2019年(x轴)的收入图(y轴),并希望能够更改显示的团队。目前,每个团队都会同时显示,这使得图形很难理解。我不想改变x轴或y轴,只是每次显示的数据点对应于一个单一的团队 我使用的代码是一个非交互式图形,一次显示每个数据点,如下所示:R 与非';t在x或y轴上,r,shiny,R,Shiny,我不是100%确定如何正确地问这个问题,但我希望解决的问题应该非常简单 我感兴趣的是在R中创建一个交互式散点图,它的变化不是基于为x轴或y轴选择一个新值,而是来自数据集中的另一个变量 具体而言,我希望为给定团队绘制1998-2019年(x轴)的收入图(y轴),并希望能够更改显示的团队。目前,每个团队都会同时显示,这使得图形很难理解。我不想改变x轴或y轴,只是每次显示的数据点对应于一个单一的团队 我使用的代码是一个非交互式图形,一次显示每个数据点,如下所示: ui <- fluidPage(
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)
ui您只需要将正在绘制的数据帧子集。试试这个
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)
库(gapminder)
同一队