-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSummer_REU_R_Quick_Look.Rmd
182 lines (120 loc) · 3.99 KB
/
Summer_REU_R_Quick_Look.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
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
---
title: "Summer_REU"
author: "Drew A. Clinkenbeard"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# R Quick look
## Syntax
Hopefully everyone had a chance to work through the Swirl lessons. If so this will be a refresher/reminder. If not then the following will help us get started.
This document i a live document. Documents like this may be used to generate reports, or to show how we arrived at our conclusions. It may also be used to allow others to replicate our results.
### Comments
When writing code it is a good idea to add in comments. In R comments start with a hash tag.
```{r}
# this is a comment.
# Lines may be commented by pressing cmd+shit+c / ctrl+shift+c
# This shortcut will also uncomment lines.
```
## Variables
As we work with data it makes sense to store values for future reference. These can be the results of calculations or, really, anything.
```{r}
# You've probably guessed by now but the {r} is indicating the the following is R code.
# We should be familiar with setting a variable in r:
x <- 6
# We can also do so going the other way
7 -> y
# We can also use the more tradtional =
z = x * y
x
y
z
```
## Navigation
We can use the IDE to navigate around R or we can use commands. The commands we have seen are:
```{r}
getwd() # shows the current working directory
dir() # lists all the files in the current directory. We can also do this with list.files()
newDir = "demo_dir"
oldDir = getwd()
dir.create(newDir) # What do we think this is doing?
dir()
setwd(newDir)
getwd()
setwd(oldDir)
```
## Reading in Data
There are a number of built in data sets in R but generally speaking we will often be analyzing our own data. To do that we will need to read our data into a variable.
```{r}
supers <- read.csv('https://raw.githubusercontent.com/dclinkenbeard/summer-intro-to-coding-2022/main/SuperHero.csv')
```
```{r}
summary(supers)
```
```{r}
mean(supers$Height)
```
If there is a particular subset of data we are interested in, we can use the 'subset' function to select those data.
```{r}
supers.dc <- subset(supers, Universe=="DC")
supers.mcu <- subset(supers, Universe=="MCU")
summary(supers.dc)
summary(supers.mcu)
```
```{r}
plot(supers$Height, type="b")
```
## More Advanced Plotting
The built in plots in R are very useful and easy to use. Sometimes, however, we need to do more. For this we need to install some packages. In this case we are going to use ggplot2.
Remove hashes in code to install packages. We are going to install a package called tidyverse. This contains many data analysis tools including ggplot2.
```{r}
install.packages("tidyverse")
library(tidyr)
```
## Using ggplot2
```{r}
MCU.height.power <- ggplot(supers.mcu, aes(x=Power, y=Height))
plot(MCU.height.power)
```
```{r}
MCU.height.power <- MCU.height.power + geom_point()
plot(MCU.height.power)
```
```{r}
eyePlot <- ggplot(supers)+geom_bar(aes(x=Eye_Color))
plot(eyePlot)
```
```{r}
eyePlot <- ggplot(supers) +
geom_bar(aes(x=Eye_Color, fill=Eye_Color)) +
scale_fill_brewer()
plot(eyePlot)
```
```{r}
eyePlot <- ggplot(supers) +
geom_bar(aes(x=Eye_Color, fill=Eye_Color)) +
scale_fill_manual(
values=c("Blue","Brown","Green","Grey","Red","Yellow")
)
plot(eyePlot)
```
```{r}
eyePlot <- ggplot(supers) +
geom_bar(aes(x=Eye_Color, fill=Eye_Color)) +
scale_fill_manual( name="Legend",
values=c("Blue"="Blue","Brown"="Brown","Green"="Green","Grey"="Grey","Red"="Red","Yellow"="Yellow")
)
plot(eyePlot)
```
### Choosing colors
Here we are using the value of the ordinal data to set the color. It goes value=color for a list of colors see: <http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf>
```{r}
eyePlot <- ggplot(supers[order(supers.mcu$Height),]) +
geom_bar(aes(x=Eye_Color, fill=Eye_Color)) +
scale_fill_manual( name="Legend",
values=c("Blue"="Dark Blue","Brown"="chocolate","Green"="#00ff00","Grey"="lightslategrey","Red"="Red","Yellow"="Yellow2")
)
plot(eyePlot)
```