2017-02-22 24 views
2

我試圖使用ggalluvial來跟蹤學生在學期的學術路徑,看看學生隨着時間的推移如何改變課程。如何將單個路徑映射到沖積圖?

這是我的數據集的樣本:

structure(list(id = c("1", "2", "6", "8", "9", "10", "11", "12", 
"14", "15", "1", "2", "6", "8", "9", "10", "11", "12", "14", 
"15", "1", "2", "6", "8", "9", "10", "11", "12", "14", "15", 
"1", "2", "6", "8", "9", "10", "11", "12", "14", "15", "1", "2", 
"6", "8", "9", "10", "11", "12", "14", "15", "1", "2", "6", "8", 
"9", "10", "11", "12", "14", "15", "1", "2", "6", "8", "9", "10", 
"11", "12", "14", "15", "1", "2", "6", "8", "9", "10", "11", 
"12", "14", "15"), 
curr = c("CURR1", "CURR1", "CURR1", "CURR1", 
    "CURR1", "CURR1", "CURR1", "CURR1", "CURR1", "CURR1", "CURR3", 
    "CURR3", "CURR3", "CURR3", "CURR3", "CURR3", "CURR3", "CURR3", 
    "CURR3", "CURR3", "CURR5", "CURR5", "CURR5", "CURR5", "CURR5", 
    "CURR5", "CURR5", "CURR5", "CURR5", "CURR5", "CURR7", "CURR7", 
    "CURR7", "CURR7", "CURR7", "CURR7", "CURR7", "CURR7", "CURR7", 
    "CURR7", "CURR9", "CURR9", "CURR9", "CURR9", "CURR9", "CURR9", 
    "CURR9", "CURR9", "CURR9", "CURR9", "CURR11", "CURR11", "CURR11", 
    "CURR11", "CURR11", "CURR11", "CURR11", "CURR11", "CURR11", "CURR11", 
    "CURR13", "CURR13", "CURR13", "CURR13", "CURR13", "CURR13", "CURR13", 
    "CURR13", "CURR13", "CURR13", "CURR15", "CURR15", "CURR15", "CURR15", 
    "CURR15", "CURR15", "CURR15", "CURR15", "CURR15", "CURR15"), 
     value = c("ISDS", "ISDS", "GBUS", "ISDS", "GBUS", "ISDS", 
     "ACCT", "GBUS", "ITF", "MGT", "ISDS", "ISDS", "GBUS", "ISDS", 
     "MKT", "ISDS", "ACCT", "GBUS", "ITF", "MGT", "ISDS", "ISDS", 
     "ISDS", "ISDS", "MKT", "ISDS", "ACCT", "GBUS", "ISDS", "MGT", 
     "ISDS", "ISDS", "ISDS", "ISDS", "ISDS", "ISDS", "ACCT", "GBUS", 
     "ISDS", "ISDS", "ISDS", "ISDS", "ISDS", NA, "ISDS", "ISDS", 
     "ACCT", "ISDS", "ISDS", "ISDS", "ISDS", "ISDS", "ISDS", "ISDS", 
     "ISDS", "ISDS", "ISDS", "ISDS", "ISDS", "ISDS", "ISDS", NA, 
     "ISDS", "ISDS", "ISDS", NA, "ISDS", "ISDS", "ISDS", "ISDS", 
     "ISDS", NA, "ISDS", "ISDS", "ISDS", NA, "ISDS", "ISDS", "ISDS", 
     NA)), class = "data.frame", row.names = c(NA, -80L), .Names = c("id", 
    "curr", "value")) 
  • ID =憑學生證
  • CURR =學期ID
  • 值=課程編號

我要地圖:

  1. CURR(時間變量),以x軸

  2. value於y軸的不同高度

  3. value計數每個CURR到的流的寬度

這張圖應該顯示他們隨着時間的流逝而流入哪個課程。

這是我到目前爲止,這是非常關

ggplot(as.data.frame(ff2), 
     aes(x=curr, axis1=value, group=id)) + 
    geom_alluvium(aes(fill = value)) 

enter image description here

x軸看起來還好,但weight不隨時間和我反映課程的不同權重可以跟隨學生的「流動」。

+0

我的第一個建議是排序變量。將CURR更改爲數字或至少一個字母數字順序的字符串。 'ff2 $ curr < - as.numeric(gsub(「CURR」,「」,ff2 $ curr))' 至於數值,我不太確定y軸的高度是什麼意思。 – jesstme

+0

@jesstme我想在y軸上有課程名稱。至於訂購,我可以使用'有序',但這並不能解決問題。 – Dambo

回答

2

對不起。我合併了一個包含獨立幾何的實驗分支,用於繪製軸之間的「流」,而不是跨越整個圖的全部「alluvia」,以及一些新參數。假設ff2在OP中被分配了structure()調用,這使得您可以使用以下代碼描述可能出現的情節。

# keep the values of 'curr' in their proper order 
ff2$curr <- factor(ff2$curr, levels = unique(ff2$curr)) 
ggplot(ff2, aes(
    # position aesthetics: 
    # 'x' as in 'geom_bar()' 
    # 'stratum' and 'alluvium' specific to ggalluvial 
    x = curr, stratum = value, alluvium = id, 
    # apply 'fill' colors to both flows and strata 
    fill = value 
)) + 
    # flow parameters: 
    # 'lode.guidance' says how to arrange splines in each stratum 
    # 'aes.flow' says which axis determines flow aesthetics 
    geom_flow(lode.guidance = "rightleft", aes.flow = "forward") + 
    geom_stratum() + 
    # include text labels at each stratum 
    geom_text(stat = "stratum") 

感謝您指出這方面的需要,特別是對於以一致的方式處理NA小號!

0

請嘗試以下操作。這不是華麗的,但它的作品。你可以使用基礎圖形來清理它。

安裝以下軟件包,如果你還沒有準備好,然後加載它們:

library(alluvial) 
library(tidyr) 

編輯您的數據:

ff2$value[is.na(ff2$value)] <- "None" # Replace NAs with a category so they're not lost 
ff2$curr <- as.numeric(substr(ff2$curr, 5, nchar(ff2$curr))) # Change your term labels to numeric for easy & correct ordering 
ff3 <- spread(ff2, curr, value, fill = "None") #spread your df from long to wide format 

彩色圖表的學生,更容易跟蹤隨着時間的推移:

cl <- colors(distinct = TRUE) 
color_palette <- sample(cl, length(ff3$id)) 

用地描述:

alluvial(ff3[,2:9], 
     freq = 8, 
     col = color_palette, 
     blocks = T, 
     xw = 0.2,# makes the ribbons a bit wavier 
     axis_labels = c("Term1","Term2", "Term3","Term4","Term5","Term6", "Term7","Term8"))