2017-02-28 61 views
-1

我的目標中的第一步已經實現(謝謝@akrun),但我似乎無法完成目標線!繼續我的第一個很快得到解答的問題,我仍然無法創建直方圖。使用LimeSurvey API響應創建的數據框的動態R直方圖

這是什麼,我想我的動力輸出,看起來像一個工作示例:

library(dplyr) 
library(ggplot2) 
library(tidyr) 
df <- data.frame(timeline = Sys.Date() - 1:10, 
       q3 = sample(c("Yes", "No"), size = 10, replace = T), 
       q4 = sample(c("Yes", "No"), size = 10, replace = T), 
       q5 = sample(c("Yes", "No"), size = 10, replace = T), 
       q6 = sample(c("Yes", "No"), size = 10, replace = T), 
       q7 = sample(c("Yes", "No"), size = 10, replace = T), 
       q8 = sample(c("Yes", "No"), size = 10, replace = T), 

       stringsAsFactors = F) %>% 
    mutate(q3 = ifelse(q3 == "Yes", 1, 0), 
      q4 = ifelse(q4 == "Yes", 1, 0), 
      q5 = ifelse(q5 == "Yes", 1, 0), 
      q6 = ifelse(q6 == "Yes", 1, 0), 
      q7 = ifelse(q7 == "Yes", 1, 0), 
      q8 = ifelse(q8 == "Yes", 1, 0) 

    ) %>% 
    gather(key = question, value = value, q3, q4, q5, q6, q7, q8) 

g <- ggplot(df, aes(x = timeline, y = value, fill = question)) + 
    geom_bar(stat = "identity") 

g 

,這是我得到了help後,這是一個被創建數據幀的例子。我不知道列的數量,他們的名字,或者觀察的數量,所以它必須動態地工作。

Appetite <- c("No","Yes","No","No","No","No","No","No","No") 
Dental.Health <- c("No","Yes","No","No","No","No","Yes","Yes","No") 
Dry.mouth <- c("No","Yes","Yes","Yes","Yes","No","Yes","Yes","No") 
Mouth.opening <- c("No","No","Yes","Yes","Yes","No","Yes","Yes","No") 
Pain.elsewhere <- c("No","Yes","No","No","No","No","No","No","No") 
Sleeping <- c("No","No","No","No","No","Yes","No","No","No") 
Sore.mouth <- c("No","No","Yes","Yes","No","No","No","No","No") 
Swallowing <- c("No","No","No","No","Yes","No","No","No","No") 
Cancer.treatment <- c("No","No","Yes","Yes","No","Yes","No","No","No") 
Support.for.my.family <- c("No","No","Yes","Yes","No","No","No","No","No") 
Fear.of.cancer.coming.back <- c("No","No","Yes","Yes","No","No","Yes","No","No") 
Intimacy <- c("Yes","No","No","No","No","No","No","No","No") 
Dentist <- c("No","Yes","No","No","No","No","No","No","No") 
Dietician <- c("No","No","Yes","Yes","No","No","No","No","No") 
Date.submitted <- c("2002-07-25 00:00:00", 
       "2002-09-05 00:00:00", 
       "2003-01-09 00:00:00", 
       "2003-01-09 00:00:00", 
       "2003-07-17 00:00:00", 
       "2003-11-06 00:00:00", 
       "2004-12-17 00:00:00", 
       "2005-06-03 00:00:00", 
       "2005-12-17 00:00:00") 

theDates <- as.Date(Date.submitted, "%Y-%m-%d %T") 

theDataFrame <- data.frame(timeline = as.list(theDates), 
          Appetite, 
          Dental.Health, 
          Dry.mouth, 
          Mouth.opening, 
          Pain.elsewhere, 
          Sleeping, 
          Sore.mouth, 
          Swallowing, 
          Cancer.treatment, 
          Support.for.my.family, 
          Fear.of.cancer.coming.back, 
          Intimacy, 
          Dentist, 
          Dietician, 
          stringsAsFactors = F) %>% 
    theDataFrame[-1] <- lapply(theDataFrame[-1], function(x) as.integer(x=="Yes")) %>% 
    gather(key = question, value = value, as.list(theDataFrame)) 

go <- ggplot(theDataFrame, aes(x = timeline, y = value, fill = question)) + 
    geom_bar(stat = "identity") 

go 

我可能誤解了收集工作,但我似乎無法得到我的頭。

+0

你想實現什麼?你的工作例子適用於我。你想改變什麼? – ottlngr

+0

我需要得到第二個工作。我創建的數據框是從API調用返回給LimeSurvey的示例。因爲會有不同數量的變量和觀測值,所以必須動態工作(不知道任何列的數量/名稱) –

回答

0

In his comment,OP請求讓第二個例子工作。這可以用一些修改OP的代碼來完成:

library(tidyr) 
library(ggplot2) 

Appetite <- c("No","Yes","No","No","No","No","No","No","No") 
Dental.Health <- c("No","Yes","No","No","No","No","Yes","Yes","No") 
Dry.mouth <- c("No","Yes","Yes","Yes","Yes","No","Yes","Yes","No") 
Mouth.opening <- c("No","No","Yes","Yes","Yes","No","Yes","Yes","No") 
Pain.elsewhere <- c("No","Yes","No","No","No","No","No","No","No") 
Sleeping <- c("No","No","No","No","No","Yes","No","No","No") 
Sore.mouth <- c("No","No","Yes","Yes","No","No","No","No","No") 
Swallowing <- c("No","No","No","No","Yes","No","No","No","No") 
Cancer.treatment <- c("No","No","Yes","Yes","No","Yes","No","No","No") 
Support.for.my.family <- c("No","No","Yes","Yes","No","No","No","No","No") 
Fear.of.cancer.coming.back <- c("No","No","Yes","Yes","No","No","Yes","No","No") 
Intimacy <- c("Yes","No","No","No","No","No","No","No","No") 
Dentist <- c("No","Yes","No","No","No","No","No","No","No") 
Dietician <- c("No","No","Yes","Yes","No","No","No","No","No") 
Date.submitted <- 
    c("2002-07-25 00:00:00", "2002-09-05 00:00:00", "2003-01-09 00:00:00", 
    "2003-01-09 00:00:00", "2003-07-17 00:00:00", "2003-11-06 00:00:00", 
    "2004-12-17 00:00:00", "2005-06-03 00:00:00", "2005-12-17 00:00:00") 

theDataFrame <- data.frame(
    timeline = as.Date(Date.submitted), 
    Appetite, Dental.Health, Dry.mouth, Mouth.opening, Pain.elsewhere, 
    Sleeping, Sore.mouth, Swallowing, Cancer.treatment, Support.for.my.family, 
    Fear.of.cancer.coming.back, Intimacy, Dentist, Dietician, 
    stringsAsFactors = FALSE) 

theDataFrame[-1] <- lapply(theDataFrame[-1], function(x) as.integer(x=="Yes")) 
theDataFrame <- theDataFrame %>% gather(question, value, -timeline) 

go <- ggplot(theDataFrame, aes(x = timeline, y = value, fill = question)) + 
    geom_col() 
go 

enter image description here

相關問題