-
Notifications
You must be signed in to change notification settings - Fork 3
/
20181222_ggplot2.R
58 lines (43 loc) · 2.02 KB
/
20181222_ggplot2.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
library(gapminder)
library(dplyr)
library(ggplot2)
# Create gapminder_1952 by filtering for the year 1952
gapminder_1952 <- gapminder %>% filter(year == 1952)
glimpse(gapminder_1952)
head(gapminder_1952)
#ScatterPlot
ggplot(gapminder_1952, aes(x = gdpPercap, y = lifeExp)) +
geom_point()
#Log Scales
ggplot(gapminder_1952, aes(x = gdpPercap, y = lifeExp)) +
geom_point() + scale_x_log10()
# Adding Color & Size Aesthetic
ggplot(gapminder_1952, aes(x = pop, y = lifeExp, color = continent, size = gdpPercap)) +
geom_point() +
scale_x_log10()
# Creating a subgraph for each continent
# Scatter plot comparing gdpPercap and lifeExp, with color representing continent
# and size representing population, faceted by year
gapminder %>% ggplot(aes(x=gdpPercap, y=lifeExp, color=continent,size=pop)) + geom_point() +
facet_wrap(~year) + scale_x_log10()
# Summarize the median gdpPercap by year & continent, save as by_year_continent
by_year_continent <- gapminder %>% group_by(year, continent) %>%
summarise(medianGdpPercap = median(gdpPercap))
head(by_year_continent)
# Create a line plot showing the change in medianGdpPercap by continent over time
by_year_continent %>% ggplot(aes(x = year, y = medianGdpPercap, color = continent)) +
geom_line() + expand_limits(y = 0)
# Summarize the median gdpPercap by year and continent in 1952
by_continent <- gapminder %>% filter(year == 1952) %>% group_by(continent) %>%
summarise(medianGdpPercap = median(gdpPercap))
head(by_continent)
# Create a bar plot showing medianGdp by continent
by_continent %>% ggplot(aes(x = continent, y = medianGdpPercap)) +
geom_col()
# Create a histogram of population (pop), with x on a log scale
gapminder_1952 %>% ggplot(aes(x = pop)) +
geom_histogram() + scale_x_log10()
# Add a title to this graph: "Comparing GDP per capita across continents"
ggplot(gapminder_1952, aes(x = continent, y = gdpPercap)) +
geom_boxplot() +
scale_y_log10() + ggtitle(label = "Comparing GDP per capita across continents")