-
Notifications
You must be signed in to change notification settings - Fork 3
/
ggplot2_key.R
50 lines (37 loc) · 1.29 KB
/
ggplot2_key.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
library(tidyverse)
library(gapminder)
# Simple scatter plots ----------------------------------------------------
# Asia Life Expectancy in 1992
data %>%
filter(continent == "Asia", year == 1992) %>%
ggplot(aes(x = lifeExp, y = reorder(country, lifeExp))) +
geom_point()
# Africa and Europe Life expectancy in 2007
data %>%
filter(continent %in% c("Europe", "Africa") &
year == 2007) %>%
ggplot(aes( x= lifeExp, y = reorder(country, lifeExp),
color = continent)) +
geom_point() +
facet_wrap(~continent)
# GDP of Americas and Europe in 2002
data %>%
filter(continent %in% c("Americas", "Europe") &
year == 2002) %>%
ggplot(aes(x = gdpPercap, y = reorder(country, gdpPercap), color = continent)) +
geom_point()
# GDP of each continent in 2007
data %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarize(GDP = mean(gdpPercap)) %>%
ggplot(aes(x = continent, y = GDP)) +
geom_col()
# Explicitly change the order of 'categorical' factors
data %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarize(GDP = mean(gdpPercap)) %>%
mutate(continent = factor(continent, levels = c("Oceania", "Africa", "Asia", "Europe"))) %>%
ggplot(aes(x = continent, y = GDP)) +
geom_col()