A Visualization Of The 100 Greatest Love Songs ft. D3.js

What would you do? If my heart was torn in two (More Than Words, Extreme)

Playing with rCharts package I had the idea of representing the list of 100 best love songs as a connected set of points which forms a heart. Songs can be seen putting mouse cursor over each dot:

You can reproduce it with this simple code:

library(dplyr)
library(rCharts)
library(rvest)
setwd("YOUR WORKING DIRECTORY HERE")
heart <- function(r,x) {ifelse(abs(x)<2, ifelse(r%%2==0, sqrt(1-(abs(x)-1)^2), acos(1-abs(x))-pi), 0)} data.frame(x=seq(from=-3, to=3, length.out=100)) %>% 
  mutate(y=jitter(heart(row_number(), x), amount=.1)) -> df
love_songs <- html("http://www.cs.ubc.ca/~davet/music/list/Best13.html") love_songs %>%
  html_nodes("table") %>%
  .[[2]] %>%
  html_table(header=TRUE, fill = TRUE) %>%
  cbind(df) -> df
m1=mPlot(x = "x",  y = "y",  data = df,  type = "Line")
m1$set(pointSize = 5, 
       lineColors = c('red', 'red'),
       width = 850,
       height = 600,
       lineWidth = 2,
       hoverCallback = "#! function(index, options, content){
       var row = options.data[index]
       return '<b>' + row.ARTIST + '</b>' + '<br/>' + row.TITLE} !#",
       grid=FALSE,
       axes=FALSE)
m1$save('Top_100_Greatest_Love_Songs.html', standalone = TRUE)

4 thoughts on “A Visualization Of The 100 Greatest Love Songs ft. D3.js

  1. When I execute the final command, it gave me an error:

    > m1$save(‘Top_100_Greatest_Love_Songs.html’, standalone = TRUE)
    Error in splitter_a(.data, .margins, .expand) : Invalid margin

    > sessionInfo()
    R version 3.2.2 (2015-08-14)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 7 x64 (build 7600)

    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
    [5] datasets methods base

    other attached packages:
    [1] reshape2_1.4.1 base64enc_0.1-3
    [3] rvest_0.3.0 xml2_0.1.2
    [5] rCharts_0.4.5 dplyr_0.4.3
    [7] devtools_1.9.1

    loaded via a namespace (and not attached):
    [1] Rcpp_0.12.1 lattice_0.20-33
    [3] digest_0.6.8 assertthat_0.1
    [5] grid_3.2.2 R6_2.1.1
    [7] plyr_1.8.3 DBI_0.3.1
    [9] magrittr_1.5 httr_1.0.0
    [11] stringi_0.5-5 curl_0.9.3
    [13] whisker_0.3-2 RJSONIO_1.3-0
    [15] tools_3.2.2 stringr_1.0.0
    [17] yaml_2.1.13 parallel_3.2.2
    [19] memoise_0.2.1

    1. Don’t really know what happens. Maybe you can try to update rCharts package? Are you using RStudio? Do you want me to send you my session info?

Leave a Reply to Thọ Duy Nguyễn Cancel reply

Your email address will not be published. Required fields are marked *