Problem 1 [20 points]

Create a plotly graph of your choosing that represents at least two variables, one of which must be a categorical variable.

This plot can be a scatter plot, overlayed density plots (graphing variable is continuous, separate densities grouped by categorical variable), etc. choropleth maps could also be on the list…you have to admit they look kinda cool.

You can use any data of your choosing, just reference this.

The graph must include:

  1. customized hover text that is informative to the graphing elements in the plot

  2. separate color to represent groups

  3. labeled axes and appropriate title

Include at least a 1-paragraph discussion about the graph. Discuss what is being plotted and what information is being displayed in the graph. Discuss any information that the reader may gain from hovering the cursor over graphing elements. Discuss any issues/chalenges you had (if any) while making the plot, and you you dealt with or overcame them.


The data utilized is the loans data per country available from https://www.kaggle.com/datasets/kinantytasya/quiz-sas-to-python?rvi=1. The features available in the data give attributes of the loans requested and approved by different countries around the world. For the case of the task, a sample of 3000 loans from the data were used and the countries with the top 10 most loans selected for further analysis and plotting using the plotly package. The scatterplot gives the lender count against the loan amount by country for the top 10 countries that seek loans in terms of frequency. The interest in the relationship between the loan amount and the lender count is the determination of how the number of lenders willing to provide credit to a country affects the loan amount that the country requests for.


Data

# Loans Data
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
loan_kiva <- read_csv("loan_kiva.csv")
## Rows: 323279 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (6): activity, sector, country, region, currency, repayment_interval
## dbl  (6): id, funded_amount, loan_amount, partner_id, term_in_months, lender...
## dttm (2): posted_time, funded_time
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#Taking a sample of the first 3000 observations
loan_kiva <- loan_kiva[1:3000, ]

# Taking countries with top 10 highest frequency in the data
loan_kiva_t10 <- loan_kiva %>%
  add_count(country) %>% 
  filter(n %in% tail(sort(unique(n)),10)) %>% 
  arrange(desc(n))
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot_ly(data = loan_kiva_t10, 
        x = ~lender_count, 
        y = ~loan_amount, 
        color = ~country, 
        hoverinfo = "text",
        hovertext = paste("Country :", loan_kiva_t10$country,
                          "<br> Lender Count :", loan_kiva_t10$lender_count,
                          "<br> Loan Amount :", loan_kiva_t10$loan_amount)) %>%
  layout(
    xaxis = list(title = 'Lender Count'),
    yaxis = list(title = 'Loan Amount'),
    title = 'Lender Count against Loan Amount by Country'
  )
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

Problem 2 [20 points]

Create an animated plotly graph with a data set of your choosing. This can be, but does not have to be a scatter plot. Also, the animation does not have to take place over time. As mentioned in the notes, the frame can be set to a categorical variable. However, the categories the frames cycle through should be organized (if needs be) such that the progression through them shows some pattern.

This graph should include:

  1. Aside from the graphing variable, a separate categorical variable. For example, in our animated scatter plot we color grouped the points by continent.

  2. Appropriate axis labels and a title

  3. Augment the frame label to make it more visible. This can include changing the font size and color to make it stand out more, and/or moving the frame label to a new location in the plotting region. Note, if you do this, make sure it is till clearly visible and does not obstruct the view of your plot.

Include at least a 1-paragraph discussion about the plot. Discuss what you are plotting and what trends can be seen throughout the animation. Discuss any issues, if any, you ran into in making the plot and how you overcame them.


The animated scatterplot gives the lender count against the loan amount by repayment interval for the top 10 countries that seek loans in terms of frequency. The interest in the relationship between the loan amount and the lender count is the determination of how the number of lenders willing to provide credit to a country affects the loan amount that the country requests for with the repayment interval equally considered. The plotly animation transitioned over the 10 countries so that the relationship between the loan amount and the lender count can be observed as well with the repayment interval distribution displayed for each of the countries.


Link to RPubs: https://rpubs.com/Sayannolla93/1173513


plot_ly(data = loan_kiva_t10, 
        x = ~lender_count, 
        y = ~loan_amount, 
        color = ~repayment_interval, 
        frame = ~country,
        hoverinfo = "text",
        hovertext = paste("Country :", loan_kiva_t10$country,
                          "<br> Lender Count :", loan_kiva_t10$lender_count,
                          "<br> Loan Amount :", loan_kiva_t10$loan_amount)) %>%
  layout(
    xaxis = list(title = 'Lender Count'),
    yaxis = list(title = 'Loan Amount'),
    title = 'Lender Count against Loan Amount by Country'
  ) %>%
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  )
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
## replace is not a multiple of replacement length

What to turn in: