首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R中与非x或y轴变量交互的散点图

R中与非x或y轴变量交互的散点图
EN

Stack Overflow用户
提问于 2021-04-11 00:03:23
回答 1查看 31关注 0票数 0

我不是百分之百确定如何恰当地提出这个问题,但我希望解决的问题应该是相当简单的。

我感兴趣的是在R中制作交互式散点图,它不是基于为x轴或y轴选择新值,而是从数据集中的另一个变量进行更改。

具体地说,我希望从1998年至2019年(x轴)为给定团队的收入(y轴),并希望能够改变哪个团队正在显示。目前,每个团队都是同时显示的,这使得图表很难理解。我不希望x轴或y轴发生变化,只希望每次显示的数据点对应于单个团队。

我使用的代码,结果是一个非交互式的图形,同时显示每个数据点,如下所示:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-11 03:10:43

您只需要将正在绘制的数据框设为子集。尝尝这个

代码语言:javascript
复制
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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67036453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档