## Homework 2: Data Visualization and Helpful
Libraries
# load gapminder data set
data <- read.csv("gapminder.csv")
# filter data by year 2007
data_2007 <- data %>%
filter(year == 2007)
# run esquisse package on 2007 data
# esquisse::esquisser(data_2007)
if (interactive()) {
esquisse::esquisser(data_2007, viewer = NULL)
}
# underlying esquisse plot code
ggplot(data_2007) +
aes(
x = gdpPercap,
y = lifeExp,
colour = continent,
size = pop
) +
geom_point() +
scale_color_hue(direction = 1) +
scale_x_continuous(trans = "log10") +
labs(x = "Income", y = "Lifespan") +
theme_minimal()
# save esquisse plot
# knitr::include_graphics(rep("esquisse-plot.png", 1))
# build faceted plot
xyplot(lifeExp ~ log(gdpPercap) | continent, data = data_2007,
main = "Scatter Plot of Life Expectancy vs GDP per Capita by Continent",
xlab = "GDP Per Capita (log scale)",
ylab = "Life Expectancy",
auto.key = list(title = "continent"))
# create colored version of GDP vs Life Exp
hchart(data_2007, "scatter", hcaes(x = log(gdpPercap), y = lifeExp, group = continent)) %>%
hc_title(text = "Scatter Plot of Life Expectancy vs GDP per Capita by Continent") %>%
hc_xAxis(title = list(text = "GDP Per Capita (log scale)")) %>%
hc_yAxis(title = list(text = "Life Expectancy"))
# create plotly version of Life Exp vs GDP
plot_ly(data = data_2007, x = ~log(gdpPercap), y = ~lifeExp, color = ~continent,
type = "scatter", mode = "markers", marker = list(size = 10)) %>%
layout(title = "Scatter Plot of Life Expectancy vs GDP per Capita by Continent",
xaxis = list(title = "GDP Per Capita (log scale)"),
yaxis = list(title = "Life Expectancy"))