Import the Socrative quiz bank for this module with SOC-46399671
---------- Socrative #1 ----------: Test everything in Zoom
ggplot-workshop
data
, and figures
(I also add analyses
)data
folder.gap <- read.csv("data/gapminder_data.csv")
?read.csv
head(gap)
str(gap)
Question: How many of you have made a plot in R? How many of you have used ggplot?
library(ggplot2)
Grammar of graphics: Every plot is a dataset, a coordinate system, and a set of layers that are the visual representation
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp))
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point()
---------- Socrative #2 ----------: Modify a ggplot graph
ggplot(data = gap, mapping = aes(x = year, y = lifeExp,
color = continent)) +
geom_point()
ggplot(data = gap, mapping = aes(x = year, y = lifeExp,
group = country, color = continent)) +
geom_line()
ggplot(data = gap, mapping = aes(x = year, y = lifeExp,
group = country, color = continent)) +
geom_line() +
geom_point()
ggplot(data = gap, mapping = aes(x = year, y = lifeExp,
group = country)) +
geom_line(mapping = aes(color = continent)) +
geom_point()
ggplot(data = gap, mapping = aes(x = year, y = lifeExp,
group = country)) +
geom_line(mapping = aes(color = continent)) +
geom_point(color = "blue")
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp,
color = continent)) +
geom_point()
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point(alpha = 0.5) +
scale_x_log10()
We could also set this to use a data column inside mapping = aes()
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
scale_x_log10() +
geom_smooth(method = "lm")
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
scale_x_log10() +
geom_smooth(method = "lm", size = 1.5)
---------- Socrative #3 ----------: Color by continent & add separate trends
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent), size = 0.5) +
scale_x_log10() +
geom_smooth(aes(group = continent), method = "lm")
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp,
color = continent)) +
geom_point(size = 2, mapping = aes(shape = continent)) +
scale_x_log10() +
geom_smooth(method = "lm")
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp,
color = continent)) +
geom_point(mapping = aes(shape = continent), size = 2, alpha = 0.5) +
scale_x_log10() +
geom_smooth(method = "lm") +
scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 10)) +
theme_minimal() +
labs(title = "Effects of per-capita GDP on Life Expectancy", # graph title
x = "GDP per Capita ($)", # x-axis title
y = "Life Expectancy (yrs)", # y-axis title
color = "Continents", # color legend title
shape = "Continents") # shape legend title
---------- Socrative #4 ----------: Optional build new plot & make pub quality
library(scales)
ggplot(data = gap, mapping = aes(x = pop, y = gdpPercap,
color = continent)) +
geom_point() +
scale_x_log10(labels = comma) +
scale_y_log10(labels = comma) +
theme_minimal() +
labs(x = "GDP per Capita ($)",
y = "Life Expectancy (yrs)",
color = "Continents")
ggsave(file = "figures/life_expectancy.png")
ggsave(file = "figures/life_expectancy.pdf")
---------- Socrative #5 ----------: Optional ggsave & ggplot variables
lifeExp_plot <- ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point()
lifeExp_plot
ggsave(plot = lifeExp_plot, file = "figures/life_expectancy_gdpPerCapita.pdf")
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp)) +
facet_wrap(~ continent) +
geom_point(alpha = 0.5) +
scale_x_log10() +
geom_smooth(method = "lm")
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp)) +
facet_wrap(~ continent, scales = "free", ncol = 2) +
geom_point(alpha = 0.5) +
scale_x_log10() +
geom_smooth(method = "lm")
---------- Socrative #6 ----------: Optional Facets, facet by year, color by continent
ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp,
color = continent)) +
facet_wrap(~ year) +
geom_point(alpha = 0.5) +
scale_x_log10() +
geom_smooth(method = "lm")
plotA <- ggplot(data = gap, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point(alpha = 0.5) +
scale_x_log10() +
geom_smooth(method = "lm") +
theme_cowplot()
plotA
plotB <- ggplot(data = gap, mapping = aes(x = continent, y = lifeExp)) +
geom_boxplot() +
theme_cowplot()
plotB
plot_grid(plotA, plotB, labels = c("A", "B"))
ggsave(file = "figures/test.pdf", width = 10, height = 5,
units = "in", dpi = 300)
browseVignettes("cowplot")
ggdraw() +
draw_plot(plotA, x = 0, y = 0, width = 0.3, height = 1) +
draw_plot(plotB, x = 0.3, y = 0, width = 0.7, height = 1)
library(plotly)
yearLifeExp <- ggplot(data = gap, mapping = aes(x = year, y = lifeExp,
group = country)) +
facet_wrap(~ continent) +
geom_line() +
scale_x_log10()
ggplotly(yearLifeExp)
theme(
# Text size for axis ticks
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
# Text size for axis labels
# Also move them away from the axes a bit for more space
axis.title.x = element_text(size = 18, face = "bold", vjust = -1),
axis.title.y = element_text(size = 18, face = "bold", vjust = 1.5),
# Plot title size, move away from plot
plot.title = element_text(size = 20, face = "bold", vjust = 5)
)
theme(
# Text size
legend.text = element_text(size = 14),
legend.title = element_text(size = 16, face = "bold"),
# Position
legend.position = c(x = 0.8, y = 0.2)
)
Create a boxplot showing the spread of life expectancy for each continent
ggplot(data = gap, mapping = aes(x = continent, y = lifeExp)) +
geom_boxplot() +
geom_jitter(width = 0.2, alpha = 0.5, color = "tomato")
ggplot(data = gap, mapping = aes(x = continent, y = lifeExp)) +
geom_boxplot() +
geom_jitter(width = 0.2, alpha = 0.5, size = 2,
mapping = aes(color = factor(year)))
Create a grouped barplot showing life expectancy by year for each continent
ggplot(data = gap, mapping = aes(x = continent)) +
geom_bar()
ggplot(data = gap, mapping = aes(x = continent, y = lifeExp, fill = factor(year))) +
geom_bar(stat = "summary", fun.y = "mean", position = "dodge")