forked from rstudio-conf-2020/dataviz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_get_started.Rmd
More file actions
207 lines (129 loc) · 3.35 KB
/
Copy path02_get_started.Rmd
File metadata and controls
207 lines (129 loc) · 3.35 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
---
title: "Get Started"
author: "Kieran Healy"
date: "10-January-2020"
output: html_document
---
## Load Libraries
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(gapminder)
library(here)
library(tidyverse)
library(socviz)
```
A code chunk:
```{r 02-get-started-1}
```
## Things to know about R
### Everything has a name
```{r 02-get-started-2}
```
### Everything is an object
```{r objects}
c(1, 2, 3, 1, 3, 5, 25)
```
```{r create_objects}
my_numbers <- c(1, 2, 3, 1, 3, 5, 25)
your_numbers <- c(5, 31, 71, 1, 3, 21, 6)
```
```{r 02-get-started-3}
my_numbers
```
### You do things using functions
```{r 02-get-started-4}
my_numbers
```
```{r this_will_error, eval = FALSE}
mean()
```
```{r 02-get-started-5}
mean(x = my_numbers)
mean(x = your_numbers)
```
```{r 02-get-started-6}
mean(my_numbers)
```
```{r 02-get-started-7}
my_summary <- summary(my_numbers)
```
```{r 02-get-started-8}
my_summary
```
### Functions come in libraries
```{r output}
table(my_numbers)
sd(my_numbers)
my_numbers * 5
my_numbers + 1
my_numbers + my_numbers
```
### If you're not sure what an object is, ask for its class
```{r getclass}
class(my_numbers)
class(my_summary)
class(summary)
```
```{r 02-get-started-9}
my_new_vector <- c(my_numbers, "Apple")
my_new_vector
class(my_new_vector)
```
```{r titanic1}
titanic
class(titanic)
```
```{r titanic2}
titanic$percent
```
```{r titanic3}
titanic_tb <- as_tibble(titanic)
titanic_tb
```
### To see inside an object, ask for its structure, or use RStudio's object inspector
```{r str}
str(my_numbers)
str(my_summary)
```
## Be patient with R, and with yourself
Here are three very specific things to watch out for:
- Make sure parentheses are balanced and that every opening "`(`" has
a corresponding closing "`)`".
- Make sure you complete your expressions. If you think you have completed typing your code, but instead of seeing the `>` command prompt at the console you see the `+`
character instead, that may mean R thinks you haven't written a complete
expression yet. You can hit `Esc` or `Ctrl-C` to force your way back to the console and try typing your code again.
- In ggplot specifically, as you will see, we will build up plots a
piece at a time by adding expressions to one another. When doing
this, make sure your `+` character goes at the end of the line, and
not the beginning. That is, write this:
```{r 02-get-started-10, echo=TRUE, eval=FALSE}
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point()
```
and not this:
```{r 02-get-started-11, echo=TRUE, eval=FALSE}
ggplot(data = mpg, aes(x = displ, y = hwy))
+ geom_point()
```
## Get data into R
Remotely:
```{r 02-get-started-12, echo = TRUE, eval = FALSE}
url <- "https://cdn.rawgit.com/kjhealy/viz-organdata/master/organdonation.csv"
organs <- read_csv(file = url)
```
Or locally:
```{r 02-get-started-13}
organs <- read_csv(file = "data/organdonation.csv")
organs
```
## Make your first figure
```{r gapminder-example-1}
gapminder
```
```{r first_plot, fig.height=6, fig.width=10, fig.cap="Life expectancy plotted against GDP per capita for a large number of country-years."}
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point() +
geom_smooth(mapping = aes(color = continent, fill = continent)) +
scale_x_log10(labels = scales::dollar)
```