Global settings

These are some plot related settings which we can setup right now. These settings will be fed to plot_ly() later on. Also, having these aesthetic settings accessible now will make it easier for us to make changes later on.

cols<-RColorBrewer::brewer.pal(nrow(df),"Set1")# Set of colors (n = number of rows in data)

opacity<-0.5# Opacity of ideogram

chord.opacity<-0.3# Opcaity of individual chords

linecolor<-"black"

circlefill<-"#f2f2f2"

inner.radius<-0.93

gap<-0.02

Creating the Ideogram

We’ll first create the ideogram. The ideogram which is essentially a set of sectors plotted on a unit circle which’ll represent the rowsums of the dataset i.e. the total number of comments made by each person (to themselves and their friends). We’ll first create some helper functions – toAngular() to map a vector of numeric values onto the unit circle using cumulative sums essentially creating sectors and addGaps() which’ll create some space between each sector for aesthetic purposes.

# Use group numbers assigned to each sector to subset and create a path string

ideogram.path<-by(ideogram,ideogram$group,createPath)

# Plot the ideogram (just as a check). Chord diagram is generated separately later

# Create shape list

ideogram.shapes<-list()# Used later on

for(iin1:nrow(df)){

# Use plotly syntax to save shapes of each sector as a list

ideogram.shapes[[i]]<-list(type="path",

path=ideogram.path[i],

fillcolor=cols[i],

line=list(color=linecolor,width=1),

opacity=opacity)

}

# Just to check if things are looking okay

ideogram.plot<-plot_ly(height=800,width=800)%>%

layout(

xaxis=list(showgrid=F,zeroline=F,showticklabels=F),

yaxis=list(showgrid=F,zeroline=F,showticklabels=F),

shapes=ideogram.shapes)

ideogram.plot

You should now have something similar to this:

Creating Chords

The following set of code snippets essentially do this:

Divide each sector on the ideogram into sub – sectors based on the number of comments made by each person i.e. traverse the dataset row-wise and map the numeric vectors in each row onto the associated sector (not the unit circle). Example: Emma has a total of 65 comments amongst herself and her friends. The sector corresponding to Emma’s 65 comments needs to be divided into further sectors based on the 16, 3, 28, 0 and 18 comments.

Find the four points that bind each chord. A chord is a planar shape that is bound by two bezier curves and two circular arcs. For each bezier curve the control point is generated by finding the mean of the angluar coordinates of the end points.

Fill each chord (ribbon) with the appropriate color. Example: Emma made 18 comments on Sophia’s posts but Sophia made 23 comments on Emma’s posts. Since Sophia made more comments, the chord depicting the interaction betweem Emma and Sophia is colored using the same color that is used for coloring Sophia’s sector on the ideogram.

Create SVG shapes for each chord and then plot using plot_ly()

Note that the ordering of the endpoints of bezier curves and circular arcs is tricky and was done based on trial and error.