簡體   English   中英

R Shiny-如何使用“融化”功能(reshape2程序包)創建堆疊的條形圖

[英]R Shiny - How to use the “melt” function (reshape2 package) to create a stacked barplot

感謝您的回答,我設法制作了一個根據時間單位(周,月,年)做出反應的條形圖,並按時間單位(鏈接在此處)聚合數據: R Shiny-如何創建根據時間單位(周,月,年)並按時間單位匯總數據

然后,我希望制作一個帶有兩個變量的堆積條形圖。 為此,我生成了帶有兩個變量的跟隨數據框(例如,在我的示例中:Imported_cases和Autochthonous_cases),然后應用“ melt”函數。 用戶界面在這里:

library(shiny)
library(dplyr)
library(lubridate)
library(ggplot2)
library(scales)
library(reshape2)

Disease <- data.frame(
  Date = seq(as.Date("2015/1/1"), as.Date("2017/1/1"), "days"),
  Imported_cases = rep(1),Autochtonous_cases=rep(2))
Disease <- Disease %>% mutate(
  Week = format(Date, "%Y-%m-%U"),
  Month = format(Date, "%Y-%m"), Year = format(Date, "%Y"))
Disease<- melt(Disease, id = c("Date","Week","Month","Year"), 
               measured = c("Imported_cases", "Autochtonous_cases"))
print(head(Disease))

ui <- fluidPage(
      dateRangeInput("daterange", "Choice the date",
                     start = min(Disease$Date),
                     end   = max(Disease$Date),
                     min   = min(Disease$Date),
                     max   = max(Disease$Date),
                     separator = " - ", format = "dd/mm/yy",
                     startview = 'Month', language = 'fr', weekstart = 1),
      selectInput(inputId = 'Time_unit',
                  label = 'Time_unit',
                  choices = c('Week', 'Month', 'Year'),
                  selected = 'Month'),
                  plotOutput("Disease"))

當我運行服務器時,R Shiny顯示:找不到錯誤對象“變量”。 您在下面找到服務器代碼:

server <- function(input, output) {
dateRangeInput <- reactive({
dataset <- subset(
  Disease, Date >= input$daterange[1] & Date <= input$daterange[2])
dataset
})

selectInput = reactive({
dataset <- dateRangeInput() %>% group_by_(input$Time_unit) %>% 
  summarise(Sum = sum(value))
dataset
})

output$Disease <-renderPlot({
                ggplot(data=selectInput(), 
                aes_string(x = input$Time_unit, y = "Sum", 
                           fill = "variable"))  + 
                geom_bar(stat = "identity")
                })

}
shinyApp (ui = ui, server = server)

我不知道問題是selectInput的代碼還是output$Disease的代碼。 我不明白為什么Shiny找不到“變量”(請參閱​​print(head(Disease))。謝謝您的幫助(希望您能清楚地說)。

上面的代碼將起作用並創建堆積的條形圖:

library(shiny) 
library(dplyr)
library(lubridate)
library(ggplot2)
library(scales)
library(reshape2)

Disease<-data.frame(Date=seq(as.Date("2015/1/1"), as.Date("2017/1/1"), "days"),Cases=rep(1),Autochtonous_cases=rep(2))
Disease <- Disease %>% mutate(Week = format(Date, "%Y-%m-%U"),Month = format(Date, "%Y-%m"), Year = format(Date, "%Y"))
Disease<-melt(Disease,id=c("Date","Week","Month","Year")) # just id


ui <- fluidPage(
  dateRangeInput("daterange", "Choice the date",
                 start = min(Disease$Date),
                 end = max(Disease$Date),
                 min = min(Disease$Date),
                 max = max(Disease$Date),
                 separator = " - ", format = "dd/mm/yy",
                 startview = 'Month', language = 'fr', weekstart = 1),
  selectInput(inputId = 'Time_unit',
              label='Time_unit',
              choices=c('Week','Month','Year'),
              selected='Month'),
  plotOutput("Disease"))


server <- function(input, output) {
  dateRangeInput<-reactive({
    dataset <- subset(Disease, Date >= input$daterange[1] & Date <= input$daterange[2])
    dataset
  })
  selectInput= reactive({
    dataset <- dateRangeInput() %>% group_by_(input$Time_unit,"variable") %>% summarise(Sum = sum(value)) #I have added here grouping as variable
    print(head(dataset))
    dataset
  })

  output$Disease <-renderPlot({
    ggplot(data=selectInput(), aes_string(x=input$Time_unit,y="Sum", fill = "variable"))  + geom_bar(stat="identity") + 
      labs(title="Disease", y ="Number of cases") +
      theme_classic() + 
      theme(plot.title = element_text(hjust = 0.5))
  })

}
shinyApp (ui = ui, server = server)

我想這就是您要尋找的。 您在melt函數中犯了一些小錯誤,僅設置id變量就足夠了,第二件事是考慮在group_by_創建的變量列(因為您想獲取個案和自治個案的數量),最后是使用變量作為fill ggplot參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM