Advertisement
Guest User

Untitled

a guest
Jul 10th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. df <- read.csv("ps-income-shares.csv")
  2.  
  3. library("rCharts")
  4. p <- dPlot(
  5. value ~ Year,
  6. groups = c("Fractile"),
  7. data = transform(df, Year = as.character(format(as.Date(Year), "%Y"))),
  8. type = "line",
  9. bounds = list(x = 50, y = 50, height = 300, width = 500)
  10. )
  11.  
  12. p$yAxis(type = "addMeasureAxis", showPercent = TRUE)
  13.  
  14. library("ggplot2")
  15. library("scales")
  16. p <- ggplot(data = df, aes(x = Year, y = value, color = Fractile))
  17. p <- p + geom_line()
  18. p <- p + theme_bw()
  19. p <- p + scale_x_date(limits = as.Date(c("1911-01-01", "2023-01-01")), labels = date_format("%Y"))
  20. p <- p + scale_y_continuous(labels = percent)
  21. p <- p + theme(legend.position = "none")
  22. p <- p + geom_text(data = subset(df, Year == "2012-01-01"), aes(x = Year, label = Fractile, hjust = -0.2), size = 4)
  23. p <- p + xlab("")
  24. p <- p + ylab("")
  25. p <- p + ggtitle("U.S. top income shares (%)")
  26. p
  27.  
  28. p$xAxis(inputFormat = '%Y-%m-%d', outputFormat = '%Y')
  29. p$yAxis(outputFormat = "%")
  30. p$setTemplate(afterScript = "
  31. <script>
  32. x.timeField = 'Year'
  33. x.timePeriod = d3.time.years
  34. x.timeInterval = 10
  35. myChart.draw()
  36. myChart.axes[0].titleShape.remove() // remove x label
  37. myChart.axes[1].titleShape.remove() // remove y label
  38. myChart.svg.append('text') // chart title
  39. .attr('x', 40)
  40. .attr('y', 20)
  41. .text('U.S. top income shares (%)')
  42. .style('text-anchor','beginning')
  43. .style('font-size', '100%')
  44. .style('font-family','sans-serif')
  45. </script>
  46. ")
  47. p
  48.  
  49. myChart.axes[1].titleShape.text('Year')
  50.  
  51. p$set(width = 1000, height = 600)
  52. p$legend(
  53. x = 580,
  54. y = 0,
  55. width = 50,
  56. height = 200,
  57. horizontalAlign = "left"
  58. )
  59.  
  60. p$save("ps-us-top-income-shares.html", cdn = TRUE)
  61.  
  62. df <- read.csv("ps-income-shares.csv")
  63. p <- dPlot(
  64. value ~ Year,
  65. groups = c("Fractile"),
  66. data = df,
  67. type = "line",
  68. bounds = list(x = 50, y = 50, height = 300, width = 500)
  69. )
  70. p$xAxis(inputFormat = '%Y-%m-%d', outputFormat = '%Y')
  71. p$yAxis(outputFormat = "%")
  72. p$setTemplate(afterScript = "
  73. <script>
  74. myChart.axes[0].timeField = 'Year'
  75. myChart.axes[0].timePeriod = d3.time.years
  76. myChart.axes[0].timeInterval = 5
  77. myChart.draw()
  78.  
  79. //if we wanted to change our line width to match the ggplot chart
  80. myChart.series[0].shapes.style('stroke-width',1);
  81.  
  82. </script>
  83. ")
  84. p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement