library(gapminder)
library(gganimate)
library(gifski)
income_per_person <- read.csv("https://raw.githubusercontent.com/rk2991/sta553/main/data/income_per_person.csv")
life_expectancy <- read.csv("https://raw.githubusercontent.com/rk2991/sta553/main/data/life_expectancy_years.csv")
country_region <- read.csv("https://raw.githubusercontent.com/rk2991/sta553/main/data/countries_total.csv")
population <- read.csv("https://raw.githubusercontent.com/rk2991/sta553/main/data/population_total.csv")
# Reshape data set: Income Per Person to make a longitudinal data such that the resulting data set has three columns: country, year, and income.
income_long <- income_per_person %>%
mutate(Country=geo)%>%
gather(key = "Year", value = "Income", -geo , -Country , na.rm=TRUE) %>%
mutate(year = as.numeric(substr(Year,2,5))) %>%
select(-Year,-geo)
head(income_long)
# Do the same for Life Expectancy in Years so that the resulting data set has three columns: country, year, and life expectancy.
life_expectancy_long <- life_expectancy %>%
mutate(Country=geo)%>%
gather(key = "Year", value = "lifeExp", -geo, -Country, na.rm = TRUE) %>%
mutate(year = as.numeric(substr(Year,2,5))) %>%
select(-Year,-geo)
head(life_expectancy_long)
# combine the two tables together
LifeExpIncom <- merge(income_long, life_expectancy_long, by= c('Country','year'))
head(LifeExpIncom)
# Merge LifeExpIncom with country region so that the final data set has information about income, life expectancy, and country region
LifeExpIncomRegion <- merge(LifeExpIncom, country_region, by.x = "Country", by.y = "name", all.x = FALSE)
LifeExpIncomRegion <- select(LifeExpIncomRegion, year, Country, region, Income, lifeExp)
head(LifeExpIncomRegion)
# Reshaping Population data set
population_long <- population %>%
mutate(Country=geo)%>%
gather(key = "Year", value = "Population", -geo , -Country ,na.rm = TRUE) %>%
mutate(year = as.numeric(substr(Year,2,5))) %>%
select(-Year,-geo)
final_merge <- merge(LifeExpIncomRegion, population_long, by = c("Country", "year"), all.x = FALSE)
head(final_merge)
Interactive scatter plot to display the association between life expectancy and income for the year 2015.Following instructions are followed:
country_data_2015 <- final_merge %>%
filter(year == 2015)%>%
mutate(ppm = Population / 1000000) %>%
select(Country, year, lifeExp, Income, Population, ppm)
head(country_data_2015)
plot_ly(
data = country_data_2015,
x = ~Income, # Horizontal axis
y = ~lifeExp, # Vertical axis
color = ~factor(Country), # must be a numeric factor
stroke = I("black"),
hoverinfo = "text",
hovertext = paste("Country: ", country_data_2015$Country,
"<br> Population: ", country_data_2015$ppm, "Million"),
alpha = 0.8,
size = ~ppm,
sizes = c(50, 1000),
type = "scatter",
mode = "markers" )%>%
layout(title = "Association Between Life Expectancy & Income in 2015",
titlefont = list(
size = 20,
color = 'darkred'),
margin = 8,
plot_bgcolor = "#E2E2E2",
xaxis = list(title = 'Income (US$)',erolinewidth = 1.5,
gridcolor = 'white'),
yaxis = list(title = 'Life Expectancy (Yr)',zerolinewidth = 1.5,
gridcolor = 'white'),
showlegend = FALSE)
Animated scatter plot showing pattern of change in the relationship between life expectancy and income over the years (considered the years from 1940 to 2018).The following instructions are followed:
The following instructions are followed:
poc <- read_csv("https://raw.githubusercontent.com/rk2991/sta553/main/data/POC.csv")
poc_Sample <- poc[sample(nrow(poc), 500), ]
# geo styling
geostyle <- list(scope = 'usa',
projection = list(type = 'albers usa'),
showland = TRUE,
landcolor = toRGB("gray95"),
subunitcolor = toRGB("gray85"),
countrycolor = toRGB("gray85"),
countrywidth = 0.5,
subunitwidth = 0.5
)
## plotting map
fig <- plot_geo(poc_Sample, lat = ~ycoord, lon = ~xcoord) %>%
add_markers( text = ~paste("<br><b>State: </b>", STATE,
"<br><b>County: </b>", county,
"<br><b>Address: </b>", ADDRESS,
"<br><b>Zip code: </b>", ZIPnew),
color = "red",
symbol = I("circle"),
size = I(8),
hoverinfo = "text" ) %>%
layout( title = list( text = "500 Randomly Gas stations in the United States",
font = list( size = 18,
color = "darkred" )),
geo = geostyle )
fig