The presidential
data set in ggplot2 package contains the names of each president, the start and end date of their term, and their party of 11 US presidents from Eisenhower to Obama. Draw the following plot.
library(ggplot2)
library(dplyr)
knitr::kable(presidential)
name | start | end | party |
---|---|---|---|
Eisenhower | 1953-01-20 | 1961-01-20 | Republican |
Kennedy | 1961-01-20 | 1963-11-22 | Democratic |
Johnson | 1963-11-22 | 1969-01-20 | Democratic |
Nixon | 1969-01-20 | 1974-08-09 | Republican |
Ford | 1974-08-09 | 1977-01-20 | Republican |
Carter | 1977-01-20 | 1981-01-20 | Democratic |
Reagan | 1981-01-20 | 1989-01-20 | Republican |
Bush | 1989-01-20 | 1993-01-20 | Republican |
Clinton | 1993-01-20 | 2001-01-20 | Democratic |
Bush | 2001-01-20 | 2009-01-20 | Republican |
Obama | 2009-01-20 | 2017-01-20 | Democratic |
presidential %>%
mutate(id = 33 + 1:nrow(presidential)) %>%
ggplot(aes(start, id, colour = party)) +
geom_point() +
geom_segment(aes(xend = end, yend = id)) +
scale_colour_manual(values = c(Republican = "red", Democratic = "blue")) +
theme_bw()
Using the gapminder dataset, gg(animate) the temporal evolution of GDP per capita versus life expectancy for all countries in continent Asia in reverse chronological order. Observe the animation and draw your conclusions or issue some questions.
library(ggplot2)
library(gganimate)
library(gapminder)
library(dplyr)
gapminder %>%
filter(continent == "Asia") %>%
ggplot(aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.5, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 20)) +
scale_x_log10() +
theme_minimal() +
# Here comes the gganimate specific bits
labs(title = 'Year: {-frame_time}', x = 'GDP per capita (log)', y = 'life expectancy') +
transition_time(-year) +
ease_aes('linear') +
shadow_wake(0.20, wrap = FALSE)
anim_save("gapminderAsia.gif")
library(gapminder)
library(dplyr)
library(ggplot2)
library(shiny)
ui = fluidPage(
titlePanel("Gapminder Data"),
sidebarLayout(
sidebarPanel(
# Year input
sliderInput("year",
"Year",
min = 1952,
max = 2007,
step = 5,
value = 1952),
# Continent input
selectInput("continent", "Continent",
choices = list("Africa", "Americas", "Asia", "Europe", "Oceania", "All"),
selected = "All"),
# x input
radioButtons("xaxis", "X",
choices = list("Life expectancy" = "lifeExp",
"Population" = "pop",
"GDP per capita" = "gdpPercap"),
selected = "gdpPercap"),
# y input
radioButtons("yaxis", "Y",
choices = list("Life expectancy" = "lifeExp",
"Population" = "pop",
"GDP per capita" = "gdpPercap"),
selected = "lifeExp")
),
# scatter plot output
mainPanel(
plotOutput("scatterPlot")
)
)
)
server <- function(input, output) {
output$scatterPlot <- renderPlot({
# generate dataset
if (input$continent == "All") {
dataset = filter(gapminder, year == input$year)
} else {
dataset = filter(gapminder, year == input$year, continent == input$continent)
}
# draw the scatter plot
ggplot(dataset, aes_string(input$xaxis, input$yaxis, size = "pop", colour = "country")) +
geom_point(alpha = 0.5, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 20)) +
scale_x_log10() +
scale_y_log10() +
theme_minimal()
})
}
shinyApp(ui = ui, server = server)
Use HTML widgets plotly and DataTables to plot and table the gapminder dataset for continent Africa and year 2007.
library(ggplot2)
library(plotly)
library(gapminder)
library(dplyr)
dataset = filter(gapminder, continent == "Africa", year == 2007)
p = dataset %>%
ggplot(aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.5, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 10)) +
scale_x_log10() +
theme_minimal()
ggplotly(p)
library(DT)
datatable(dataset, options = list(pageLength = 10))