0

I've created a cluster in R from a data frame using the code below:

myvars <- c("member_screen_name", "friend_screen_name", "party")
newdata <- total[myvars]
#just using 30 rows
x <- newdata[sample(nrow(newdata), 30), ]
g <- graph_from_data_frame(x, directed = FALSE)
plot(g)

I'm trying to change the color of each vertex according to their "party" association, either Democratic (D) or Republican (R). I know I can change the color of all vertices by doing the following:

plot(g,vertex.color='red')

How do I change the color of each vertex according to the "party"?

Jacob3454
  • 175
  • 1
  • 9
  • 2
    Easy way is to add a column of the desired colors onto your dataframe "g" and then pass that to the plot function: `plot(g, vertex.color=g$color)` – Dave2e Nov 26 '20 at 19:34
  • Also, I recommend reading this help page: https://stackoverflow.com/help/someone-answers – Dave2e Nov 26 '20 at 19:53
  • your example is confusing. Is party the party of the member or of the friend? You should have two dataframes, one with the edges, and one with the vertices and their attributes. You pass in both of them to ```graph_from_data_frame```. If the vertices dataframe has a column named "color", the graph will display colors by default. See here for an example https://stackoverflow.com/questions/61758388/igraph-set-node-color-to-categorical-vertex-attribute/61768035#61768035 – desval Nov 27 '20 at 10:41

0 Answers0