# Loading the datasets
data(mtcars)
data(gapminder)
Question 1
# Create a scatter plot with overlayed density plots
plot_ly(data = mtcars, x = ~mpg, y = ~disp, color = ~cyl, type = "scatter") %>%
add_trace(x = ~mpg, type = "histogram", opacity = 0.5, histnorm = "probability density") %>%
layout(
title = "MTCARS Dataset: MPG vs Displacement",
xaxis = list(title = "Miles Per Gallon (mpg)"),
yaxis = list(title = "Displacement (cu in)"),
hovermode = "closest",
hoverlabel = list(
bgcolor = "white",
font = list(color = "black")
)
) %>%
style(hoveron = "points", hoverinfo = "text", text = ~paste(
"Cylinders: ", cyl,
"<br>MPG: ", mpg,
"<br>Displacement: ", disp
))
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
This graph is like a visual guide to how far different cars can travel on a single gallon of gas (MPG) compared to the size of their engines (displacement). Each dot on the graph represents a car, and its position shows how thirsty for gas it is (MPG) and how powerful its engine is (displacement). The color of each dot is like a secret code, revealing how many cylinders the car’s engine has. If you’re curious about a specific car, you can click on its dot to unlock even more details!
Making this graph wasn’t as simple as snapping a photo of your car collection. A challenge was that some cars group together because they have similar gas mileage and engine size. To untangle this cluster, the graph uses color to represent the number of cylinders, acting like name tags for each group of cars. This lets you see how cars with different engine configurations stack up against each other in terms of gas mileage and engine power.
Conclusion, this graph is a treasure trove of information for anyone interested in the relationship between engine size, gas mileage, and car design. The color coding and clickable dots add another layer of interactivity, making it a fun and informative way to explore how these factors influence each other.
Question 2
# Preparing the data
filtered_data <- gapminder %>%
filter(year == 2007) # Filter to include data for the year 2007
# Creating a scatter plot with bubble sizes representing population
plot_ly(data = filtered_data, x = ~gdpPercap, y = ~lifeExp, color = ~continent,
type = "scatter", mode = "markers", size = ~pop, sizes = c(5, 100)) %>%
layout(
title = "Life Expectancy vs GDP Per Capita (2007)",
xaxis = list(title = "GDP Per Capita"),
yaxis = list(title = "Life Expectancy"),
showlegend = TRUE,
hovermode = "closest",
hoverlabel = list(
bgcolor = "white",
font = list(color = "black")
)
) %>%
style(hoverinfo = "text", text = ~paste(
"Country: ", country,
"<br>GDP Per Capita: ", gdpPercap,
"<br>Life Expectancy: ", lifeExp,
"<br>Population: ", pop
))
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
This scatter plot visualizes the relationship between life expectancy and GDP per capita across various world regions in 2007. Each data point represents a country, color-coded according to its region (Africa, Americas, Asia, Europe, or Oceania).
A clear positive correlation emerges from the data. Countries with higher GDP per capita generally exhibit longer life expectancies, while those with lower GDP per capita tend to have shorter lifespans. This trend manifests across all regions, with data points forming a discernible, upward-sloping pattern.
However, the plot also reveals significant intra-regional variation. Countries with similar GDP per capita levels can have a range of life expectancies. This suggests that factors beyond economic development, such as healthcare infrastructure, social policies, and environmental quality, also significantly influence life expectancy.
In conclusion, this plot provides a valuable data-driven representation of the intricate interplay between economic prosperity and human well-being across the globe. It highlights the importance of GDP per capita but acknowledges the influence of other determinants in shaping life expectancy across different countries.