Site icon R-bloggers

24 Days of R: Day 12

[This article was first published on PirateGrunt » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Today, I'm going to finally dip my toe in the water of something I've wanted to do for a long time. I'd wanted to do this with Michael Caine film appearances- and I may yet- but tonight, it'll be a bit simpler. I'm going to draw a network graph, showing the relationship between band members of the Grateful Dead and a couple of their solo and non-Dead projects. Information is from my head or wikipedia, reflecting the most recent band lineups.

First, I'll create data frames for several bands.

dfGratefulDead = data.frame(Musician = c("Jerry Garcia", "Bob Weir", "Phil Lesh", 
    "Bill Kreutzmann", "Mickey Hart", "Pigpen"), Instrument = c("guitar", "guitar", 
    "bass", "drums", "drums", "keyboards"))

dfJerryGarciaBand = data.frame(Musician = c("Jerry Garcia", "John Kahn", "Melvin Seals", 
    "David Kemper", "Jaclyn LaBranch", "Gloria Jones"), Instrument = c("guitar", 
    "bass", "keyboards", "drums", "vocals", "vocals"))

I've created data frames for RatDog and the Other Ones as well, but the code and results are not shown. Next, I'll code a helper function to create relationships between band members.

CreateRelation = function(dfBand, BandName) {

    indices = combn(length(dfBand$Musician), 2)

    dfRelate = data.frame(from = dfBand$Musician[indices[1, ]], to = dfBand$Musician[indices[2, 
        ]])
    dfRelate$Band = BandName

    dfRelate

}

With that in place, I can create relationships and then rbind the results. I tried adding a color column for a musicians instrument, but haven't had any luck in being able to apply it in the plot. Suggestions are welcome.

dfGDrelate = CreateRelation(dfGratefulDead, "GratefulDead")
dfJGBrelate = CreateRelation(dfJerryGarciaBand, "Jerry Garcia Band")
dfRatDogRelate = CreateRelation(dfRatDog, "RatDog")
dfOtherOnesRelate = CreateRelation(dfOtherOnes, "The Other Ones")

dfMusicians = rbind(dfGratefulDead, dfJerryGarciaBand, dfRatDog, dfOtherOnes)

dfMusicians = dfMusicians[!duplicated(dfMusicians), ]

dfInstrumentColor = data.frame(Instrument = c("bass", "drums", "guitar", "keyboards", 
    "vocals", "saxophone"), Color = c("black", "beige", "red", "blue", "green", 
    "yellow"))
dfMusicians = merge(dfMusicians, dfInstrumentColor)
dfMusicians = dfMusicians[, c("Musician", "Instrument", "Color")]

dfRelations = rbind(dfGDrelate, dfJGBrelate, dfRatDogRelate, dfOtherOnesRelate)

library(igraph)

g = graph.data.frame(dfRelations, directed = FALSE, vertices = dfMusicians)

set.seed(1234)
plot(g, vertex.color = g$Color)

Note the mashup of Bobby, Mickey, Bill and Phil in the center. They've had a lot of crossover post 1995. If I didn't have to work tomorrow, I'd probably spend the next few hours playing with this. Developing a chart of all the permutations of Yes, Asia, King Crimson, etc. could take the better part of an evening.

sessionInfo()

## R version 3.0.2 (2013-09-25)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] knitr_1.4.1      RWordPress_0.2-3 xtable_1.7-1     igraph_0.6.6    
## 
## loaded via a namespace (and not attached):
## [1] digest_0.6.3   evaluate_0.4.7 formatR_0.9    RCurl_1.95-4.1
## [5] stringr_0.6.2  tools_3.0.2    XML_3.98-1.1   XMLRPC_0.3-0

To leave a comment for the author, please follow the link and comment on their blog: PirateGrunt » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.