# Load any R Packages you may need
library(mosaic)
library(tidyverse)
library(ggplot2)
library(gganimate)
library(gifski)
library(gapminder)

Exercise 1

The following exercises use the gapminder data set. Run the code chunk below to load the data and packages:

gapminder_data <- gapminder

(a)

Filter on the gapminder data to only include three countries of your choosing.

library(gapminder)

filt_count <- c("United States", "Germany", "Italy")

df <- gapminder[gapminder$country %in% filt_count, ]

print(df)
## # A tibble: 36 × 6
##    country continent  year lifeExp      pop gdpPercap
##    <fct>   <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Germany Europe     1952    67.5 69145952     7144.
##  2 Germany Europe     1957    69.1 71019069    10188.
##  3 Germany Europe     1962    70.3 73739117    12902.
##  4 Germany Europe     1967    70.8 76368453    14746.
##  5 Germany Europe     1972    71   78717088    18016.
##  6 Germany Europe     1977    72.5 78160773    20513.
##  7 Germany Europe     1982    73.8 78335266    22032.
##  8 Germany Europe     1987    74.8 77718298    24639.
##  9 Germany Europe     1992    76.1 80597764    26505.
## 10 Germany Europe     1997    77.3 82011073    27789.
## # ℹ 26 more rows

(b)

Create a scatterplot that has x as lifeExp and y as gdpPercap color coded by country and size denoted by pop.

# Type your code here 
scatterplot <- ggplot(df, aes(x = lifeExp, y = gdpPercap, color = country, size = pop)) +
  geom_point() +
  scale_size_continuous(range = c(3, 11)) +  # Adjust the range of point sizes as needed
  labs(title = "Scatterplot of lifeExp vs gdpPercap",
       x = "Life Expectancy",
       y = "GDP per Capita",
       color = "Country",
       size = "Population")

# Display the scatterplot
print(scatterplot)

(c)

Create an interactive illustration of the graph above showing the change over time. This should be an animated gif that continually loops.

# Type your code here 
animated_plot <- ggplot(df, aes(x = lifeExp, y = gdpPercap, color = country, size = pop)) +
  geom_point(alpha = 0.6) +  scale_size(range = c(3, 10)) +
  labs(title = 'Year: {frame_time}', x = 'Life Expectancy(Years)', y = 'GDP per Capita(USD)', size = "Population") +
  theme_minimal() + transition_time(year) + ease_aes('linear')
#anim <- animate(animated_plot, nframes = 200, duration = 20, end_pause = 50, renderer = gifski_renderer())
#anim_save("gapminder_anim.gif", animation = anim)
anim <- animate(animated_plot, nframes = 200, duration = 20, end_pause = 50, renderer = gifski_renderer())
anim_save("gapminder_anim.gif", animation = anim)
anim

(d)

Create an interactive illustration of the graph above showing the change over time that includes a user interface slider that allows the user to adjust the year to their choice.

# Type your code here 
library(shiny)


ui <- fluidPage(
  titlePanel("Gapminder Data Visualization with Slider"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("yearSlider",
                  "Year:",
                  min = min(df$year),
                  max = max(df$year),
                  value = min(df$year),
                  step = 5,
                  animate = animationOptions(interval = 1000, playButton = NULL, pauseButton = NULL)
      )
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

server <- function(input, output) {
  output$plot <- renderPlot({
    # Filter the data based on the selected year from the slider
    filtered_data <- df %>% filter(year == input$yearSlider)
    
    # Generate the plot
    ggplot(filtered_data, aes(x = lifeExp, y = gdpPercap, color = country, size = pop)) +
      geom_point(alpha = 0.6) +
      scale_size(range = c(3, 10)) +
      labs(title = paste('Year:', input$yearSlider), 
           x = 'Life Expectancy (Years)', 
           y = 'GDP per Capita (USD)', 
           size = "Population") +
      theme_minimal()
  })
}

# Run the Shiny app
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents

(e)

Provide a brief overview of what your visualization is showing.

In the provided ggplot code, I’ve created an interactive animated scatterplot using a dataset ‘df’. The x-axis represents ‘Life Expectancy,’ the y-axis represents ‘GDP per Capita,’ and each point is colored according to the ‘Country’ while sized by ‘Population.’ As the code progresses, it becomes evident that the animation is driven by the ‘year’ variable, showing changes in life expectancy and GDP per capita over time. The transition is smooth and linear, offering a clear visualization of the positive correlation between GDP growth and increased life expectancy. US has the high life expectancy amonst the other conuty group chosen!


Exercise 2

The following exercises will use Tableau to create an interactive dashboards using the food_data.csv file.

(a)

Create a Tableau Dashboard of the food_data.csv using Country as one of your variables. Save this to Tableau Public and then embed your code below directly into your HTML page.

(b)

Provide a brief overview of what your visualization is showing.

I’ve developed a Tableau dashboard focusing on European food consumption trends. The visual insights reveal that Ireland and England lead in tea consumption, while Holland and Luxembourg emerge as top coffee consumers. Sweden stands out as the highest consumer of red meat. To provide an overview, I’ve created a Country Heat Map highlighting top food consumers across Europe, featuring countries like Sweden and Germany among the frontrunners.”


All done!

Knit the completed R Markdown file as a HTML document (click the “Knit” button at the top of the script editor window) and upload it to the submission portal on Canvas.