简体   繁体   English

在带有 Shiny 的条形图中选择一个变量为 plot

[英]Selecting a variable to plot in a bar chart with Shiny

I have the following dataset:我有以下数据集:

在此处输入图像描述

data <- structure(list(country = c("Andorra", "Austria", "Belgium", "Denmark", "Finland", "France", "Germany", "Greece"), x48 = c(811.73, 710.0385, 765.8359, 961.434, 701.9176, 716.7308, 701.6187, 468.1942), x49 = c(655.2433, 607.8058, 628.3675, 692.426, 575.4287, 587.7711, 544.2052, 370.2103), x54 = c(2801.597, 2224.757, 2320.216, 3130.023, 2582.625, 2399.011, 2621.368, 804.5603)), class = "data.frame", row.names = c(NA, -8L))

I want to plot a double bar chart that shows, for each country in the x-axis, a bar with the values of 'x54' and a bar for either 'x48' or 'x49' like so:我想要 plot 双条形图,对于 x 轴上的每个国家/地区,一个值为“x54”的条形图和一个代表“x48”或“x49”的条形图,如下所示:

在此处输入图像描述

With the following code:使用以下代码:

library(shiny)
library(ggplot2)
library(tidyverse)

df <- read.csv("data/salario-apartamento-europa-occidental-pequeño.csv")

  ui = fluidPage(
    titlePanel("Triple Bar Chart"),
    sidebarLayout(
      sidebarPanel(
        selectInput("x1", "Type of house:", choices = names(df)[-1], selected = names(df)[1]),
      ),
      mainPanel(
        plotOutput("plot")
      )
    )
  )
  server = function(input, output) {
    output$plot <- renderPlot({
      df_long <- df %>% pivot_longer(-country)
      ggplot(data = df_long %>% 
               filter(name %in% c("x49", "x54")),  
             aes(x = country, y = value, fill = factor(name))) +
        geom_col(position = position_dodge())+
        scale_fill_manual(values = c("#58C114", "#DA560A")) +
        theme_light()  +
        theme(axis.text.x = element_text(angle = 90)) +
        scale_color_manual(name = "Metric", labels = c("1 room in the center", "Average Monthly Salary")) + 
        labs(fill =" Metric", x= "Country", y= "USD")
    })
  }

# run the app
shinyApp(ui, server)

I can do it as you can see, but you'll realize that, instead of filtering by 'x54' and the input variable "x1", I'm filtering by 'x49' thus, instead of being reactive, for me to compare 'x54' and 'x48', I have to change it manually in the code.我可以像你看到的那样做,但你会意识到,我不是按“x54”和输入变量“x1”进行过滤,而是按“x49”进行过滤,而不是被动地进行比较'x54' 和 'x48',我必须在代码中手动更改它。

I would like to have something similar to this:我想要类似这样的东西:

filter(name %in% c(input$x1, "x54"))

But unfortunatelly, this doesn't work and it only plots 'x54'.但不幸的是,这不起作用,它只绘制“x54”。

Also, how would I update the labels accordingly?另外,我将如何相应地更新标签? (Now they stay like 'x49' and 'x54' as you can see in the picture) (现在它们就像你在图片中看到的“x49”和“x54”)

It should be like so:它应该是这样的:

For 'x54': Average Monthly Salary对于“x54”:平均月薪

For 'x48': 1 room in the center对于“x48”:中心的 1 个房间

And for 'x49': 1 room outside the center对于“x49”:中心外的 1 个房间

Thank you very much in advance for the help!非常感谢您的帮助!

Your input needs to be a reactive.您的input需要是被动的。 https://mastering-shiny.org/basic-reactivity.html https://mastering-shiny.org/basic-reactivity.html

Your server would need to look more like this.您的服务器需要看起来更像这样。

server = function(input, output) {
  
  yourinput <- reactive({
    input$x1
  })
  
  output$plot <- renderPlot({
    df_long <- df %>% pivot_longer(-country)
    ggplot(data = df_long %>% 
             filter(name %in% c(yourinput(), "x54")),  
           aes(x = country, y = value, fill = factor(name))) +
      geom_col(position = position_dodge())+
      scale_fill_manual(values = c("#58C114", "#DA560A")) +
      theme_light()  +
      theme(axis.text.x = element_text(angle = 90)) +
      scale_color_manual(name = "Metric", labels = c("1 room in the center", "Average Monthly Salary")) + 
      labs(fill =" Metric", x= "Country", y= "USD")
  })
}

In the selectInput , the object used is data and not dfselectInput中,使用的 object 是data而不是df

selectInput("x1", "Type of house:", choices = names(df)[-1], 
        selected = names(df)[1]),
      ),
...

where在哪里

df <- read.csv("data/salario-apartamento-europa-occidental-pequeño.csv")

-full code -完整代码

df <- structure(list(country = c("Andorra", "Austria", "Belgium", "Denmark", "Finland", "France", "Germany", "Greece"), x48 = c(811.73, 710.0385, 765.8359, 961.434, 701.9176, 716.7308, 701.6187, 468.1942), x49 = c(655.2433, 607.8058, 628.3675, 692.426, 575.4287, 587.7711, 544.2052, 370.2103), x54 = c(2801.597, 2224.757, 2320.216, 3130.023, 2582.625, 2399.011, 2621.368, 804.5603)), class = "data.frame", row.names = c(NA, -8L))
ui = fluidPage(
  titlePanel("Triple Bar Chart"),
  sidebarLayout(
    sidebarPanel(
      selectInput("x1", "Type of house:", choices = names(df)[-1], selected = names(df)[1]),
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)
server = function(input, output) {
  output$plot <- renderPlot({
    req(input$x1)
    df_long <- df %>% pivot_longer(-country)
    
    
    ggplot(data = df_long %>% 
             filter(name %in% c(input$x1, "x54")),  
           aes(x = country, y = value, fill = factor(name))) +
      geom_col(position = position_dodge())+
      scale_fill_manual(values = c("#58C114", "#DA560A"), 
               labels= setNames( c("1 room in the center", "Average Monthly Salary"), c("x48", "x49"))) +
      theme_light()  +
      theme(axis.text.x = element_text(angle = 90)) +
       labs(fill =" Metric", x= "Country", y= "USD")
  })
}

# run the app
shinyApp(ui, server)

-output -输出

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM