Изменить порядок реактивного ggplot - PullRequest
0 голосов
/ 30 августа 2018

Я пытаюсь изменить порядок оси X на значения в оси Y. Ось X - это имя, ось Y - целое число. Оба являются реактивными, определяемыми пользователем входами. Я создал таблицу данных, которая отображается в правильном порядке, но ggplot не принимает этот порядок. Вместо этого он выполняет алфавитный порядок.

Мой текущий код:

Пакеты

library(shiny) 
library(readxl) # to load the data into R
library(tidyverse)
library(stringr)
library(DT)
library(tools)
library(magrittr)

Данные

lpop <-read.csv("londonpopchange.csv", header=TRUE)

UI

# Define UI for application that plots features of movies
ui <- fluidPage(

  # Sidebar layout with a input and output definitions
  sidebarLayout(

    # Inputs
    sidebarPanel(

      # Select variable for y-axis
      selectInput(inputId = "y", 
                  label = "Y-axis:",
                  choices = c("Mid Year 2016" = "MYE2016", 
                              "Births" = "Births", 
                              "Deaths" = "Deaths",
                              "Births minus Deaths" = "BirthsminusDeaths",
                              "Internal Migration Inflow" = "InternalMigrationInflow", 
                              "Internal Migration Outflow" = "InternalMigrationOutflow",
                              "Internal Migration Net" = "InternalMigrationNet",
                              "International Migration Inflow" = "InternationalMigrationInflow",
                              "International Migration Outflow" = "InternationalMigrationOutflow",
                              "International Migration Net" = "InternationalMigrationNet"),
                  selected = "MYE2016"),

      # Select variable for x-axis
      selectInput(inputId = "x", 
                  label = "X-axis:",
                  choices = c("Borough" = "Name"), 
                  selected = "Name")
    ),

    # Output
    mainPanel(
      h1(textOutput("MainTitle")),
      br(),
      plotOutput(outputId = "geom_bar"),
      DT::dataTableOutput("mytable")
    )
  )
)

Сервер

# Define server function required to create the scatterplot
server <- function(input, output) {

  #this creates the title
  output$MainTitle <- renderText({ 
    paste(input$y, "for London Boroughs")
  })

  #creates a data table that reacts to the user variable input and arranges
  #by the y variable
  df <- reactive({
    lpop %>%
      select(input$x, input$y, "WF") %>%
      arrange_(.dots = input$y) #%>%
    # setNames(1:2, c("x", "y"))
  })

  #outputs the user defined data frame
  output$mytable = ({DT::renderDataTable({df()})})

  # Create the bar plot object the plotOutput function is expecting
  output$geom_bar <- renderPlot({
    ggplot(data = df(), aes_string(x = input$x, y = input$y, fill = "WF")) +
      geom_bar(stat = "identity") +
      scale_fill_manual(values=c("#000000", "#00D253")) +
      theme(axis.text.x = element_text(angle = 90)) +
      xlab(input$x)
  })
}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

Это выглядит так: https://jwest.shinyapps.io/ShinyPopulation/

Если я использую функцию переупорядочения в ggplot, она объединяет все «Имена» в один столбец, см. Ниже.

# Create the bar plot object the plotOutput function is expecting
      output$geom_bar <- renderPlot({
        ggplot(data = df(), aes_string(x = reorder(input$x, input$y), y = input$y, fill = "WF")) +
          geom_bar(stat = "identity") +
          scale_fill_manual(values=c("#000000", "#00D253")) +
          theme(axis.text.x = element_text(angle = 90)) +
          xlab(input$x)
      })
    }

Picture

Как я могу отрендерить его по оси Y? Это как-то связано с scale_x_discrete (limit = ...). Если это так, я запутался в том, как я должен ссылаться на первый столбец реактивного df

CSV можно скачать здесь: https://drive.google.com/file/d/1QLT8CX9XFSx3WU_tADyWgyddHYd3-VSp/view?usp=sharing

DPUT

structure(list(Code = structure(c(7L, 1L, 12L, 13L, 14L), .Label = c("E09000001", 
"E09000002", "E09000003", "E09000004", "E09000005", "E09000006", 
"E09000007", "E09000008", "E09000009", "E09000010", "E09000011", 
"E09000012", "E09000013", "E09000014", "E09000015", "E09000016", 
"E09000017", "E09000018", "E09000019", "E09000020", "E09000021", 
"E09000022", "E09000023", "E09000024", "E09000025", "E09000026", 
"E09000027", "E09000028", "E09000029", "E09000030", "E09000031", 
"E09000032", "E09000033"), class = "factor"), Name = structure(c(6L, 
7L, 12L, 13L, 14L), .Label = c("Barking and Dagenham", "Barnet", 
"Bexley", "Brent", "Bromley", "Camden", "City of London", "Croydon", 
"Ealing", "Enfield", "Greenwich", "Hackney", "Hammersmith and Fulham", 
"Haringey", "Harrow", "Havering", "Hillingdon", "Hounslow", "Islington", 
"Kensington and Chelsea", "Kingston upon Thames", "Lambeth", 
"Lewisham", "Merton", "Newham", "Redbridge", "Richmond upon Thames", 
"Southwark", "Sutton", "Tower Hamlets", "Waltham Forest", "Wandsworth", 
"Westminster"), class = "factor"), Geography = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = "London Borough", class = "factor"), 
    MYE2016 = c(249162L, 7246L, 273239L, 181783L, 272078L), Births = c(2671L, 
    68L, 4405L, 2446L, 3913L), Deaths = c(1180L, 38L, 1168L, 
    895L, 1140L), BirthsminusDeaths = c(1491L, 30L, 3237L, 1551L, 
    2773L), InternalMigrationInflow = c(22189L, 856L, 21271L, 
    19109L, 22469L), InternalMigrationOutflow = c(25132L, 792L, 
    23324L, 20488L, 29113L), InternalMigrationNet = c(-2943L, 
    64L, -2053L, -1379L, -6644L), InternationalMigrationInflow = c(11815L, 
    756L, 5054L, 5333L, 7480L), InternationalMigrationOutflow = c(6140L, 
    441L, 3534L, 4336L, 4460L), InternationalMigrationNet = c(5675L, 
    315L, 1520L, 997L, 3020L), Other = c(-24L, -1L, -14L, 46L, 
    -3L), Estimated.Population..mid.2017 = c(253361L, 7654L, 
    275929L, 182998L, 271224L), WF = structure(c(1L, 1L, 1L, 
    1L, 1L), .Label = c("London Borough", "Waltham Forest"), class = "factor")), .Names = c("Code", 
"Name", "Geography", "MYE2016", "Births", "Deaths", "BirthsminusDeaths", 
"InternalMigrationInflow", "InternalMigrationOutflow", "InternalMigrationNet", 
"InternationalMigrationInflow", "InternationalMigrationOutflow", 
"InternationalMigrationNet", "Other", "Estimated.Population..mid.2017", 
"WF"), row.names = c(NA, 5L), class = "data.frame")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...