我想基於數據框中的額外列在iGraph中設置頂點的顏色。我在搜索中沒有看到這樣的問題。儘管我最後一次想到了,並最終提出了一個重複的問題。我的數據如下:如何根據數據框中的列設置頂點顏色iGraph?
Vertex1 Vertex2 Type
Chat Forms Other
Specials Chat Info
Form Sent Linkout Lead
Home Chat Home
我想根據類型列的內容設置頂點的值。這裏是我使用的代碼:
V(paths.g)$Type=as.character(VDP_Path5$Type[match(V(paths.g)$Type,VDP_Path5$Node1)])
V(paths.g)$color=V(paths.g)$Type
V(paths.g)$color=gsub("Lead","forest",V(paths.g)$color)
V(paths.g)$color=gsub("Vehicle","leaf",V(paths.g)$color)
V(paths.g)$color=gsub("Home","gold",V(paths.g)$color)
V(paths.g)$color=gsub("Other","silver",V(paths.g)$color)
plot(paths.g, edge.width=VDP_Path5$weight,layout=layout.fruchterman.reingold)
當我運行的情節所有的頂點是默認的青色。
謝謝
'gsub'函數有什麼作用? –
gsub是R中的模式匹配和替換函數。一般用法是'gsub(pattern,replacement,x)' – TimL
對不起,你能幫我理解他們在這種情況下做什麼嗎? –