# install.packages("gapminder") library(gapminder) getwd() # setwd("[YOUR WORKING DIRECTORY]") str(gapminder) China <- subset(gapminder, gapminder$country == "China") plot(lifeExp ~ year, data = China, xlab = "Year", ylab = "Life expectancy", main = "Life expectancy in China", col = "red", pch = 16) # install.packages("ggplot2") library(ggplot2) ggplot(data = China, aes(x = year, y = lifeExp)) + geom_point(color = "red", size = 3) + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy in China") + theme_bw(base_size=18) ggplot(data = China, #<< aes(x = year, y = lifeExp)) #<< ggplot(data = China, aes(x = year, y = lifeExp)) + geom_point() #<< ggplot(data = China, aes(x = year, y = lifeExp)) + geom_point(color = "red", size = 3) #<< ggplot(data = China, aes(x = year, y = lifeExp)) + geom_point(color = "red", size = 3) + xlab("Year") #<< ggplot(data = China, aes(x = year, y = lifeExp)) + geom_point(color = "red", size = 3) + xlab("Year") ggplot(data = China, aes(x = year, y = lifeExp)) + geom_point(color = "red", size = 3) + xlab("Year") + ylab("Life expectancy") #<< ggplot(data = China, aes(x = year, y = lifeExp)) + geom_point(color = "red", size = 3) + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy in China") #<< ggplot(data = China, aes(x = year, y = lifeExp)) + geom_point(color = "red", size = 3) + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy in China") + theme_bw() #<< ggplot(data = China, aes(x = year, y = lifeExp)) + geom_point(color = "red", size = 3) + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy in China") + theme_bw(base_size=18) #<< ggplot(data = gapminder,#<< aes(x = year, y = lifeExp)) + geom_point(color = "red", size = 3) + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy over time") + theme_bw(base_size=18) ggplot(data = gapminder, aes(x = year, y = lifeExp)) + geom_line(color = "red", size = 3) + #<< xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy over time") + theme_bw(base_size=18) ggplot(data = gapminder, aes(x = year, y = lifeExp, group = country)) + #<< geom_line(color = "red", size = 3) + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy over time") + theme_bw(base_size=18) ggplot(data = gapminder, aes(x = year, y = lifeExp, group = country)) + geom_line(color = "red") + #<< xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy over time") + theme_bw(base_size=18) ggplot(data = gapminder, aes(x = year, y = lifeExp, group = country, color = continent)) + #<< geom_line() + #<< xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy over time") + theme_bw(base_size=18) ggplot(data = gapminder, aes(x = year, y = lifeExp, group = country, color = continent)) + geom_line() + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy over time") + theme_bw(base_size=18) + facet_wrap(~ continent) #<< ggplot(data = gapminder, aes(x = year, y = lifeExp, group = country, color = continent)) + geom_line() + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy over time") + theme_bw() + #<< facet_wrap(~ continent) ggplot(data = gapminder, aes(x = year, y = lifeExp, group = country, color = continent)) + geom_line() + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy over time") + theme_bw() + facet_wrap(~ continent) + theme(legend.position = "none") #<< lifeExp_by_year <- ggplot(data = gapminder, aes(x = year, y = lifeExp, group = country, color = continent)) + geom_line() + xlab("Year") + ylab("Life expectancy") + ggtitle("Life expectancy over time") + theme_bw() + facet_wrap(~ continent) + theme(legend.position = "none") lifeExp_by_year lifeExp_by_year + theme(legend.position = "bottom") ggsave("I_saved_a_file.pdf", plot = lifeExp_by_year, height = 3, width = 5, units = "in") ggplot(data = China, aes(x = year, y = gdpPercap)) + geom_line() + scale_y_log10(breaks = c(1000, 2000, 3000, 4000, 5000)) + #<< xlim(1940, 2010) + ggtitle("Chinese GDP per capita") ggplot(data = China, aes(x = year, y = lifeExp)) + geom_line() + ggtitle("Chinese life expectancy") + theme_gray(base_size = 20) #<< lifeExp_by_year + theme(legend.position = c(0.8, 0.2)) lifeExp_by_year + theme(legend.position = c(0.8, 0.2)) + scale_color_manual( name = "Which continent are\nwe looking at?", # \n adds a line break #<< values = c("Africa" = "seagreen", "Americas" = "turquoise1", "Asia" = "royalblue", "Europe" = "violetred1", "Oceania" = "yellow")) ggplot(data = gapminder, aes(x = year, y = lifeExp, group = country)) + geom_line(alpha = 0.5, aes(color = "Country", size = "Country")) + geom_line(stat = "smooth", method = "loess", aes(group = continent, color = "Continent", size = "Continent"), alpha = 0.5) + facet_wrap(~ continent, nrow = 2) + scale_color_manual(name = "Life Exp. for:", values = c("Country" = "black", "Continent" = "blue")) + scale_size_manual(name = "Life Exp. for:", values = c("Country" = 0.25, "Continent" = 3)) + theme_minimal(base_size = 14) + ylab("Years") + xlab("") + ggtitle("Life Expectancy, 1952-2007", subtitle = "By continent and country") + theme(legend.position=c(0.82, 0.15), axis.text.x = element_text(angle = 45)) #<<