forked from amzoss/ggplot2-DF19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-output.Rmd
82 lines (60 loc) · 3.11 KB
/
5-output.Rmd
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
title: "Output"
author: "Angela Zoss"
date: "8/13/2018"
output: html_document
---
## Setup your environment
```{r}
# Load required libraries
library(tidyverse)
```
## Load your data
```{r}
gap <- read_csv("data/gapminder_avg.csv")
names(gap) <- make.names(names(gap), unique=TRUE)
```
## Create and save charts
```{r}
ggplot(gap, aes(x=Average.GDP.per.capita, y=Average.Life.expectancy.at.birth)) +
geom_point(aes(size=Average.Total.population, color=Region)) +
scale_x_log10() +
labs(x="Average GDP per capita (log 10)", y="Average life expectancy at birth", title="Averages across all years of the traditional Gapminder dataset") +
scale_size_continuous(name="Average total population", breaks=c(7500000,75000000,750000000),labels=c("7.5 million","75 million","750 million")) +
geom_smooth(method="lm", se=FALSE, color="gray50") +
geom_text(data=gap %>% filter(Average.Total.population>200000000), aes(label=Country)) +
theme_bw()
# On my machine, this creates a file 2156 pixels wide and 1332 tall; YMMV
ggsave("Figs/1-simple_save.png")
# Try setting width and height
# This uses a default unit of inches and a default dpi of 300
ggsave("Figs/2-ratio_save.png", width=3, height=2)
# That's pretty small, and on my machine text is way too large;
# let's make it big enough to fit across a letter page
ggsave("Figs/3-ratio_save2.png", width=6.5, height=4.25)
# What if we need a big version for a poster?
ggsave("Figs/4-big_save.png", width=12, height=9)
# Plenty of pixels, but the text is now way too small!
# This will look very strange in RStudio, but be patient!
ggplot(gap, aes(x=Average.GDP.per.capita, y=Average.Life.expectancy.at.birth)) +
geom_point(aes(size=Average.Total.population, color=Region)) +
scale_x_log10() +
labs(x="Average GDP per capita (log 10)", y="Average life expectancy at birth", title="Averages across all years of the traditional Gapminder dataset") +
scale_size_continuous(name="Average total population", breaks=c(7500000,75000000,750000000),labels=c("7.5 million","75 million","750 million")) +
geom_smooth(method="lm", se=FALSE, color="gray50") +
geom_text(data=gap %>% filter(Average.Total.population>200000000), aes(label=Country)) +
theme_bw(base_size=20)
# This has a nice high resolution, and the text is a more reasonable size
ggsave("Figs/5-big_base.png", width=12, height=9)
# Note: base_size scales everything up except for the geom_text layer
# can change that size separately
ggplot(gap, aes(x=Average.GDP.per.capita, y=Average.Life.expectancy.at.birth)) +
geom_point(aes(size=Average.Total.population, color=Region)) +
scale_x_log10() +
labs(x="Average GDP per capita (log 10)", y="Average life expectancy at birth", title="Averages across all years of the traditional Gapminder dataset") +
scale_size_continuous(name="Average total population", breaks=c(7500000,75000000,750000000),labels=c("7.5 million","75 million","750 million")) +
geom_smooth(method="lm", se=FALSE, color="gray50") +
geom_text(data=gap %>% filter(Average.Total.population>200000000), aes(label=Country), size=10) +
theme_bw(base_size=20)
ggsave("Figs/6-big_labels.png", width=12, height=9)
```