Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 重排序反应图_R_Ggplot2_Shiny - Fatal编程技术网

R 重排序反应图

R 重排序反应图,r,ggplot2,shiny,R,Ggplot2,Shiny,我正试图根据y轴上的值对x轴重新排序。x轴是一个名称,y轴是一个整数。两者都是用户定义的被动输入。我已经创建了一个数据表,它以正确的顺序呈现,但ggplot不采用该顺序。而是按字母顺序排列 我目前的代码是: 套餐 library(shiny) library(readxl) # to load the data into R library(tidyverse) library(stringr) library(DT) library(tools) library(magrittr) 数据

我正试图根据y轴上的值对x轴重新排序。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)
# 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)

我认为问题在于,您根据
input$y
中给出的字符串输入对字符串“Name”重新排序,因此您的x轴仅为字符串“Name”,并注意实际值。如果您提供一个最小的可复制示例并共享一些示例数据,则会更容易提供帮助。我也这么认为,但无法想出一种方法来对“Name”的值进行排序,而不仅仅是列标题。我现在已附上csv请使用您的一小部分数据样本和
dput
,并且没有用于共享的链接。现在添加dput用于重建我认为您需要设置因子的顺序,这可以在您进行反应式
df
时执行。由于将
df
按您想要的顺序放置,因此可以使用类似于
forcats::fct\u的命令来设置因子级别的显示顺序(可以在
mutate\u at()
中执行此操作,以便轻松处理字符串输入)。
# 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)
# 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)
      })
    }
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")