Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- df <- read.csv("ps-income-shares.csv")
- library("rCharts")
- p <- dPlot(
- value ~ Year,
- groups = c("Fractile"),
- data = transform(df, Year = as.character(format(as.Date(Year), "%Y"))),
- type = "line",
- bounds = list(x = 50, y = 50, height = 300, width = 500)
- )
- p$yAxis(type = "addMeasureAxis", showPercent = TRUE)
- library("ggplot2")
- library("scales")
- p <- ggplot(data = df, aes(x = Year, y = value, color = Fractile))
- p <- p + geom_line()
- p <- p + theme_bw()
- p <- p + scale_x_date(limits = as.Date(c("1911-01-01", "2023-01-01")), labels = date_format("%Y"))
- p <- p + scale_y_continuous(labels = percent)
- p <- p + theme(legend.position = "none")
- p <- p + geom_text(data = subset(df, Year == "2012-01-01"), aes(x = Year, label = Fractile, hjust = -0.2), size = 4)
- p <- p + xlab("")
- p <- p + ylab("")
- p <- p + ggtitle("U.S. top income shares (%)")
- p
- p$xAxis(inputFormat = '%Y-%m-%d', outputFormat = '%Y')
- p$yAxis(outputFormat = "%")
- p$setTemplate(afterScript = "
- <script>
- x.timeField = 'Year'
- x.timePeriod = d3.time.years
- x.timeInterval = 10
- myChart.draw()
- myChart.axes[0].titleShape.remove() // remove x label
- myChart.axes[1].titleShape.remove() // remove y label
- myChart.svg.append('text') // chart title
- .attr('x', 40)
- .attr('y', 20)
- .text('U.S. top income shares (%)')
- .style('text-anchor','beginning')
- .style('font-size', '100%')
- .style('font-family','sans-serif')
- </script>
- ")
- p
- myChart.axes[1].titleShape.text('Year')
- p$set(width = 1000, height = 600)
- p$legend(
- x = 580,
- y = 0,
- width = 50,
- height = 200,
- horizontalAlign = "left"
- )
- p$save("ps-us-top-income-shares.html", cdn = TRUE)
- df <- read.csv("ps-income-shares.csv")
- p <- dPlot(
- value ~ Year,
- groups = c("Fractile"),
- data = df,
- type = "line",
- bounds = list(x = 50, y = 50, height = 300, width = 500)
- )
- p$xAxis(inputFormat = '%Y-%m-%d', outputFormat = '%Y')
- p$yAxis(outputFormat = "%")
- p$setTemplate(afterScript = "
- <script>
- myChart.axes[0].timeField = 'Year'
- myChart.axes[0].timePeriod = d3.time.years
- myChart.axes[0].timeInterval = 5
- myChart.draw()
- //if we wanted to change our line width to match the ggplot chart
- myChart.series[0].shapes.style('stroke-width',1);
- </script>
- ")
- p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement