I'm quite new to R and having trouble with the following:
I'm researching politicians in Belgium on Twitter, and would like to see if any networks form within political parties on Twitter.
I have two data files
I want to create a graph that shows the network, but with the nodes colored by their politicial party (this variable is called 'fractie' in the data.csv file).
I've tried doing this as follows:
First, I've tried to combine the files as follows:
rownames(politicicsv) <- politicicsv[,'TwitterHandle']
test <- cbind(politicixpolitici,
politicicsv[, "Fractie"][match(rownames(politicixpolitici),
rownames(politicicsv))])
=> I've plotted this network, but it comes out very sloppy and the names are on there which makes it very hard to see + the nodes are obviously not coloured according to the party.
Then, I've tried it using statnet, but when I wanted to create the the graph, I had trouble with the creation of the vertex attribute:
fractie <- get.vertex.attribute(politicicsv, "Fractie")
Error in get.vertex.attribute(politicicsv, "Fractie") :
get.vertex.attribute requires an argument of class network.
Can someone help me in plotting this network, with the nodes colored according to the political party ("Fractie") they belong to?
Files can be found here
Thank you, this would help me with my thesis.
Can someone help me in plotting this network, with the nodes colored according to the political party ("Fractie") they belong to?
You could do it like this
df <- read.csv("data.csv")
m <- as.matrix(read.csv2("politicixpolitici.csv", row.names = 1))
library(igraph)
g <- simplify(graph_from_adjacency_matrix(m))
# Color palette:
(pal <- setNames(
colorRampPalette(categorical_pal(8))(nlevels(df$Fractie)),
levels(df$Fractie)) )
# CD&V Ecolo-Groen Groen N-VA Onafhankelijke
# "#E69F00" "#81ADA3" "#33ABB9" "#18A56E" "#C0D64B"
# Open vld Open Vld sp.a VB Vlaams Belang
# "#77AB7A" "#2A6D8E" "#BF5F11" "#CF6E64" "#BC82A2"
# Vuye&Wouters
# "#999999"
V(g)$color <- pal[df$Fractie[match(V(g)$name, df$TwitterHandle)]]
set.seed(1); coords <- layout_with_fr(g)
plot(g,
layout=coords, vertex.label.cex=.2, vertex.size=2,
edge.arrow.size=0, edge.lty="blank", asp = 0)
or try an interactive plot:
library(visNetwork)
visIgraph(g) %>%
visIgraphLayout(layout="layout.norm", layoutMatrix = coords, type = "full")
All in all I'd recommend exporting your graph to gephi and experiment with other layouts and visualizations there interactively.