Data source: United Nations World Population Prospects, the 2012 Revision
library(dplyr)
library(ggplot2)
library(plotly)
library(data.table)
full <- fread("http://esa.un.org/wpp/ASCII-Data/ASCII_FILES/WPP2012_DB02_POPULATIONS_ANNUAL.CSV")
jp <- full %>%
select(Location, Time, PopTotal, Variant) %>%
filter(Location == "Japan" & Variant == "Medium") %>%
mutate(PopM = round(PopTotal * 1000, 2),
proj = ifelse(Time < 2013, "Observed",
ifelse(Time > 2012, "Projected", "Other")))
jpplot <- ggplot(jp, aes(x = Time, y = PopM, linetype = proj)) +
geom_line(size = 1, color = "red") +
theme_bw() +
xlab("Year") +
ylab("Population (millions)") +
ggtitle("Observed and projected population change in Japan, 1950-2100") +
labs(linetype = "")
## Be sure to replace the arguments with your Plotly username and API key
py <- plotly(your_username, your_api_key)
py$ggplotly()
And here’s the chart (I moved the legend, changed the font, and added an annotation using the Plotly web GUI):