-
Notifications
You must be signed in to change notification settings - Fork 8
/
gapminder_analysis.R
executable file
·65 lines (46 loc) · 1.76 KB
/
gapminder_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
download.file("https://raw.githubusercontent.com/swcarpentry/r-novice-gapminder/gh-pages/_episodes_rmd/data/gapminder-FiveYearData.csv", destfile = "data/gapminder-FiveYearData.csv")
gapminder <- read.csv("data/gapminder-FiveYearData.csv")
mean(gapminder$lifeExp)
max(gapminder$year)
gapminder$year == 2007
gapminder2007 <- gapminder[gapminder$year == 2007,]
mean(gapminder2007$lifeExp)
mean(gapminder$lifeExp)
plot(gapminder$lifeExp,gapminder$gdpPercap)
# start of ggplot2 section
library(tidyverse)
ggplot(data=gapminder,aes(x=gdpPercap,y=lifeExp)) +
geom_point()
ggplot(data=gapminder,aes(x=year,y=lifeExp)) +
geom_point()
ggplot(data=gapminder,aes(x=year,y=lifeExp,by=country,color=continent)) +
geom_point()+
geom_line()
ggplot(data=gapminder,aes(x=gdpPercap,y=lifeExp, color=continent)) +
geom_point() +
scale_x_log10()+
geom_smooth(method="lm")
ggplot(data=gapminder[gapminder$continent=="Americas",],aes(x=gdpPercap,y=lifeExp, color=continent, by=country)) +
geom_point() +
scale_x_log10()+
geom_smooth(method="lm")+
facet_wrap( ~ country)
ggplot(data=gapminder[gapminder$continent=="Americas",],aes(x=gdpPercap,y=lifeExp, color=continent, by=country)) +
geom_point() +
scale_x_log10()+
geom_smooth(method="lm")+
facet_wrap( ~ country)
# interactive plotting with plotly
if(!require("plotly")){install.packages("plotly")}
library(plotly)
p <- ggplot(data=gapminder[gapminder$continent=="Americas",],aes(x=gdpPercap,y=lifeExp, color=continent, by=country)) +
geom_point() +
scale_x_log10()+
geom_smooth(method="lm")+
facet_wrap( ~ country)
p
ggplotly(p)
# making nice labels
p + labs(x="GDP per Capita", y="Life Expectancy (y)",
title="Figure 1") +
scale_color_discrete(name="Continent")