2016-02-21 123 views
1

如何繪製R中的條形圖並使用特定節點的特定屬性? 我有一個GML文件是這樣的:如何在R中繪製節點屬性的條形圖

graph [ 
    node [ 
    id 0 
    label "Apple" 
    year 1997 
    bold 1 
    normal 4 
    light 15 
    ] 
    node [ 
    id 1 
    label "Apple" 
    year 2000 
    bold 2 
    normal 16  
    light 2 
    ] 
    node [ 
    id 2 
    label "BBC" 
    year 2010 
    bold 18 
    normal 2 
    light 0 
    ] 
] 

和我運行該R腳本,我有麻煩時,我需要提取從一個節點選擇的屬性值:

install.packages(c("igraph")) 
library(igraph) 

g = read.graph(file = "/media/Data/TEMP/R_test/test.gml", format = "gml") 

apple = V(g)$label[V(g)$label == "Apple"] 
table(apple) 

for(i in seq(along=apple)){ 
    mL = apple[,i]$bold 
    L = apple[,i]$normal 
    pL = apple[,i]$light 

    barplot(????) 
} 

我怎麼能解決這個問題?腳本THX

回答

1

這裏是每年的加粗爲蘋果barplot:

df <- subset(as_data_frame(g, "vertices"), label=="Apple", c(year, bold)) 
barplot(df$bold, names.arg = df$year, main = "Apple") 
+0

THX,我怎麼能設置數據繪製一個** Stacked Bar **,就像每年在同一列中的屬性[粗體,正常,輕]一樣?我需要創建一個表格? – goodguyAbaddon

+1

例如'df < - subset(as_data_frame(g,「vertices」),label ==「Apple」,c(year,bold,normal,light)); barplot(as.matrix(t(df [,-1])),names.arg = df $ year,main =「Apple」)'。 – lukeA