Skip to content

Instantly share code, notes, and snippets.

@annoporci
Last active August 29, 2015 14:13
Show Gist options
  • Save annoporci/d26f09b45bd5cb9956a6 to your computer and use it in GitHub Desktop.
Save annoporci/d26f09b45bd5cb9956a6 to your computer and use it in GitHub Desktop.
df <- structure(list(Quantile = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("P90-95",
"P95-99", "P99-99.5", "P99.5-99.9", "P99.9-99.99", "P99.99-100"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Labor Income",
"Capital Income", "Mixed Income"), class = "factor"), value = c(0.88,
0.776, 0.601, 0.478, 0.366, 0.271, 0.05, 0.082, 0.151, 0.224,
0.375, 0.579, 0.07, 0.141, 0.248, 0.298, 0.26, 0.151), Levels = c(1,
2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6)), .Names = c("Quantile",
"variable", "value", "Levels"), row.names = c(NA, -18L), class = "data.frame")
# This is Figure 8.4 in Piketty, Capital in the Twenty-First Century (Harvard University Press, 2014).
# d3/nvd3 nPlot from library(rCharts)
# Below hack to print characters/factors as labels
# QUESTION: can this be automated using the data in df$Quantile?
# Note that highcharts works out of the box:
# hPlot(data = df_8_4, value ~ Quantile, group = 'variable', color = 'variable', shape = 'variable', type = 'line')
# hPlot produces circle shapes out of the box
require(rCharts)
load("data.Rda") # data in the gist.github
# round data for rChart tooltip display
df$value <- round(df$value, 3)
df$Levels <- as.numeric(df$Quantile) # nPlot doesn't seem to accept factors
n <- nPlot(data = df, value ~ Levels, group = 'variable', color = 'variable', shape = 'variable', type = 'lineChart')
n$chart(forceY = c(0, 1))
n$yAxis(axisLabel = 'Share in total income of various fractiles')
n$chart(margin = list(left = 100, right = 40)) # make room in margins
n$xAxis(tickFormat = "#! function (x) {
ticks = ['P90-95', 'P95-99', 'P99-99.5', 'P99.5-99.9', 'P99.9-99.99', 'P99.99-100']
return ticks[x-1] ;
} !#")
n$yAxis(tickFormat = "#! function(y) {return Math.round(y*100*100)/100 + '%'} !#")
n$xAxis(axisLabel = 'Year')
n$chart(useInteractiveGuideline = TRUE)
n$chart(color = colorPalette)
n$addParams(height = 500, width = 800)
n$setTemplate(afterScript = '<style>
.nv-point {
stroke-opacity: 1!important;
stroke-width: 5px!important;
fill-opacity: 1!important;
}
</style>')
n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment