-
Notifications
You must be signed in to change notification settings - Fork 4
/
GS_R_Reference.Rmd
1534 lines (1081 loc) · 37.5 KB
/
GS_R_Reference.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
```{r, include = FALSE}
source("global_stuff.R")
```
# Coding Reference {.unnumbered}
```{r, include=FALSE}
knitr::opts_chunk$set(cache=TRUE)
```
This page contains minimal explanations and examples for common coding patterns in base R and tidyverse. Students can make content requests and contribute to this reference page, just leave a message on the github issues for this course repository.
## Base R
Base R refers to intrinsics capabilities of R that come with a fresh installation of R. No additional libraries are needed.
---
## Variables
`a` is the variable name. `<-` is the assignment operator. In this example, `1` is being assigned into the object named `a`.
```{r}
a <- 1
```
Variables have classes that describe their contents.
```{r}
x <- 1
class(x)
y <- "1"
class(y)
z <- TRUE
class(z)
```
Classes allow and disallow some commands. For example, you can't add a numeric to a character:
```{r, error=TRUE}
x+y
```
Classes can be converted:
```{r}
y <- as.numeric(y)
x+y
```
### Vectors
Vectors are 1-dimensional objects that have a name, and can hold multiple elements of the same class. The number of elements in the vector is the vector length. Manipulating vectors involves creating them, and storing, retrieving, and changing elements inside of the vector.
#### Vector Creation
There are multiple ways to create a vector
```{r}
a <- c() # create a NULL vector
a
a <- 1:5 # assign a sequence to a name
a
a <- c(1,2,3,4,5) # assign a vector made with combine c()
a
#pre-assign an empty vector
a <- vector(mode = "numeric", length=10)
a
a <- vector(mode = "integer", length=10)
a
a <- vector(mode = "logical", length=10)
a
a <- vector(mode = "character", length=10)
a
```
`length()` returns the number of elements in the vector
```{r}
a < c(1,4,5)
length(a)
```
#### Vector Combination
It is possible to combine existing vectors together to make a new vector using `c()`.
```{r}
x <- 1:5
y <- 6:10
x
y
z <- c(x,y)
z
```
However, if you attempt to combine vectors of different classes, R will throw an error, or it will coerce (convert) one of the vectors into the class of the other.
```{r}
x <- 1:5
y <- c("a","b","c","d","e")
x
y
z <- c(x,y)
z
```
#### Vector Indexing
Vector indexing is the the process of isolating specific positions or elements of a vector. Vector indexing uses the `[]` notation.
The general syntax is `vector_name[positions]`, where `vector_name` is the name of your vector, and `positions` is a vector of positions to index.
```{r}
a <- c(23,34,45,56,67,78,89)
a[1] # returns the element in position 1
a[1:3] # returns elements in positions 1 to 3
a[c(4,5,6)]
a[c(1,1,1)]
```
Logical vectors can indicate positions. In this case, elements in TRUE positions will be returned
```{r}
a <- c(45,56,78)
a[c(TRUE, FALSE, FALSE)]
a[c(FALSE, TRUE, FALSE)]
a[c(FALSE, FALSE, TRUE)]
```
#### Vector indexing and assignment
Vector indexing can also be used to assign new elements to the indexed positions.
```{r}
a <- c(45,56,78)
a
a[3] <- 100
a
a[1:3] <- "Hello"
a
```
#### Logical indexing
Vectors can be indexing using logical comparisons (see section on logic for further explanation and examples of logical comparisons).
```{r}
a <- c(1,3,2,4,3,4)
a == 4 # create logical vector of positions containing 4
# inserting the above into a[] finds the elements equal to 4
a[a == 4] # elements equal to 4
a[a < 4] # elements less than 4
a[a <= 4] # elements less than or equal to 4
a[a != 1] # elements not equal to 1
```
### Data.frame
Data.frames are 2-d storage objects, like a table (or excel sheet), they have columns and rows.
```{r}
a <- data.frame() # make an empty data.frame
a
class(a)
```
#### Data.frame creation
A common method to create a data.frame involves adding existing vectors together. Data.frames are often also created when loading in data files that are 2-d tables. See also the section on using `dplyr` to manipulate data in dataframes. Data.frames are also similar to data.tables, and tibbles, and can usually be interchanged.
```{r}
x <- c(1,2,3)
y <- c("a","b","c")
z <- c(TRUE, TRUE,TRUE)
a <- data.frame(x,y,z)
a
```
`dim()` returns the number of rows and columns in the data.frame
```{r}
dim(a)
```
#### Indexing by column name
Each column in a data.frame has a name, and can be accessed using the `$` syntax:
```{r}
names(a)
a$x
a$y
a$z
#re-name by assigning a new vector
names(a) <- c("new_x","Why","Zee")
a
a$new_x
a$Why
a$Zee
```
#### Indexing with `[rows,columns]`
Data.frames have rows and columns, and can be indexed using `[rows,columns]` notation, where `rows` is a vector of row numbers, and `columns` is a vector of column numbers
```{r}
a
a[1,] # row 1
a[,1] # column 1
a[1:2,] # rows 1 to 2
a[,1:2] # columns 1 to 2
a[1:2,1:2] #rows 1 to 2 and columns 1 to 2
a[1:2,'new_x'] # Column names can be used
```
#### row and column binding
It is possible to add more rows using `rbind()`, and add more columns using `cbind()`.
```{r}
# row bind a copy of a to itself
a
a <- rbind(a,a)
dim(a)
# create a new vector, add it as a new column
my_new <- c(1,4,3,2,4,5)
a <- cbind(a,my_new)
a
```
#### Indexing and assignment
The elements in a data.frame can be re-assigned by `your_dataframe[row:position] <- new stuff`. It is generally necessary that any new elements have the same class as the original elements
```{r}
a
a[,1] <- 5 #assign column 1 all 5s
a$Why <- c("new","words","are","going","in","here")
a
a[6,3] <- FALSE # row 6, column 3
a
```
#### Logical indexing
It is also possible to index a data.frame with logical comparisons. For example, the following returns rows where the value in column `my_new` equals a 4
```{r}
a[a$my_new == 4,]
```
### Lists
Lists are objects that can store more arbitrary elements of any class, including vectors, dataframes, and even more lists. Lists are commonly used to store results from a model, especially when the model returns many different kinds of results in different formats.
Here we create a list with three elements, a scalar, a vector, and a dataframe.
```{r}
x <- 1
y <- c(1,2,3,4,5)
z <- data.frame(a= 1:5, b=1:5, c=1:5)
my_list <- list(x, y, z)
my_list
```
#### List indexing
Access elements in the list using `[[]]`
```{r}
my_list[[1]]
my_list[[2]]
my_list[[3]]
```
#### Named elements
Elements in a list can be given names, and indexed by name:
```{r}
x <- 1
y <- c(1,2,3,4,5)
z <- data.frame(a= 1:5, b=1:5, c=1:5)
my_list <- list(ex = x, why = y,zee= z)
my_list
my_list$ex
my_list$why
my_list$zee
my_list[["ex"]]
my_list[["why"]]
my_list[["zee"]]
```
#### Add/Remove elements in lists
It is possible to assign new names elements to a list, e.g.:
```{r}
my_list[["new_thing"]] <- 12345
my_list
#set an element to NULL removes it
my_list[["zee"]] <- NULL
my_list
```
## Logic
Logic statements are used to compare two things, or two sets of things. The output of comparison is a TRUE or FALSE statment. If many things are being compared at once, the output could be many TRUE or FALSE statements for each comparison
### equal to ==
```{r}
1==1 # is 1 equal to 1?
1==2 # is 1 equal to 2?
c(1,2,3) == c(2,1,3) # compares each element with each element
1 == c(2,1,3)
```
### not equal to !=
```{r}
1!=1 # is 1 equal to 1?
1!=2 # is 1 equal to 2?
c(1,2,3) != c(2,1,3) # compares each element with each element
1 != c(2,1,3)
```
### Greater than/ less than
```{r}
1 > 1 # is 1 greater than 1?
5 > 1 # is 5 greater than 1?
3 < 2 # is 3 less than 2?
3 < 1 # is 3 less than 1?
c(1,2,3) > c(2,1,3) # ask the question element by element
c(1,2,3) < c(2,1,3)
2 > c(1,2,3) # is greater than each of the numbers
```
### >= <=
Is something greater than or equal to something else
```{r}
1 >= 1 # is 1 greater than 1?
5 >= 1 # is 5 greater than 1?
3 <= 2 # is 3 less than 2?
3 <= 1 # is 3 less than 1?
c(1,2,3) >= c(2,1,3) # ask the question element by element
c(1,2,3) <= c(2,1,3)
2 >= c(1,2,3) # is greater than each of the numbers
```
### AND
The ampersand `&` is used for AND, which allows use to evaluate whether two or more properties are all TRUE.
```{r}
# is 16 divisible by 4 AND 8
16%%4 == 0 & 16%%8 ==0
# is 16 divisible by 4 AND 3
16%%4 == 0 & 16%%3 ==0
# is 16 divisible by 8 and 4 and 2
16%%4 == 0 & 16%%8 ==0 & 16%%2 ==0
```
### OR
The `|` is used for OR, which allows use to evaluate at least one of the properties is TRUE.
```{r}
# is 16 divisible by 4 OR 8
16%%4 == 0 | 16%%8 ==0
# is 16 divisible by 4 OR 3
# it is divisible by 4, so the answer is TRUE
# because at least one of the comparisons is TRUE
16%%4 == 0 | 16%%3 ==0
```
### TRUE FALSE
When R returns values as TRUE or FALSE, it return a logical variable. It also treats TRUE as a 1, and FALSE as a 0. In the example below we see it is possible sum up a logical variable with multiple TRUE and FALSE entries.
```{r}
c(1,2,3) == c(1,2,3)
sum(c(1,2,3) == c(1,2,3))
c(1,2,3) == c(2,1,3)
sum(c(1,2,3) == c(2,1,3))
```
## IF ELSE
A roller-coaster operator checks if people are taller than a line to see if they can ride the coaster. This is an IF ELSE control structure. IF the person is taller than the line, then they can go on the ride; ELSE (otherwise) the person can not go on the ride.
In other words, IF the situation is X, then do something; ELSE (if the situation is not X), then do something different.
IF and ELSE statements let us specify the conditions when specific actions are taken. Generally, IF and ELSE statements are used inside loops (for, or while, or repeat loops), because at each step or iteration of the loop, we want to check something, and then do something.
Consider this:
```{r}
a <- 1 # define a to be a 1
if(a==1){
print(a) # this is what happens if a==1
} else {
print("A is not 1") # this is what happens if a is not 1
}
a <- 2 # define a to be a 1
if(a==1){
print(a) # this is what happens if a==1
} else {
print("A is not 1") # this is what happens if a is not 1
}
```
Normally we find IF and ELSE in a loop like this:
```{r}
a <- c(1,0,1,0,0,0,1) # make a variable contain 1s and 0s
# write a loop to check each element in the variable
# and do different things depending on the element
for(i in a){
if(i == 1){
print("I'm a 1") # what to do when i is 1
} else {
print("I'm not a 1") # what to do when i is not 1
}
}
```
We can have multiple conditions in our if statements. See the next section on loops for more info about using loops.
```{r}
a <- c(1,2,3,1,2,0,1) # make a variable contain 1s and 0s
# write a loop to check each element in the variable
# and do different things depending on the element
for(i in a){
if(i == 1){
print("I'm a 1") # what to do when i is 1
} else if (i==2){
print("I'm a 2") # what to do when i is 2
} else if (i==3){
print("I'm a 3") # what to do when i is 3
} else {
print("I'm not any of the above") #what to do when none are true
}
}
```
## Loops
Check R help for on Control Flow `?Control`.
`for(){}`
`for(loop control){do something each iteration}`
```{r, eval=F}
for(iterator in vector){
#do something
}
```
Loop control is defined in between the parentheses. The name of the iterator is placed on the left of `in`(can be assigned any name you want, does not need to be declared in advance). During the execution of the loop, the iterator takes on the values inside the vector which is placed on the right side of `in`. Specifically, the following is happening.
Loop steps:
1. iterator <- vector[1]
2. iterator <- vector[2]
3. iterator <- vector[3]
4. etc.
The loop will automatically stop once it reaches the last item in the vector. The loop can be stopped before that using the `break` command.
```{r}
# Make a loop do something 5 times
# i is the iterator
# 1:5 creates a vector with 5 numbers in it, 1, 2, 3, 4, 5
# the loop will run 5 times, because there are five things to assign to i
for(i in 1:5){
print("hello")
}
```
```{r}
# show the value of i each step of the loop
for(i in 1:5){
print(i)
}
```
```{r}
# define the vector to loop over in advance
x <- 1:5
for(i in x){
print(i)
}
```
```{r}
# Reminder that i becomes the next value in the vector
# your vector can have any order
my_sequence <- c(1,5,2,3,4)
for(i in my_sequence){
print(i)
}
```
```{r}
# index vector does not need to be numbers
my_things <- c("A","B","C","D")
for(i in my_things){
print(i)
}
```
### Breaking a loop
`break` stops a loop. Used with logical statements to define the conditions necessary to cause the break.
```{r}
for(i in 1:10){
if(i <5){
print(i)
} else{
break
}
}
```
### While loops
While loops run until a logical condition is met. Here there is no iterator, just a logic statement that needs to be met.
This one prints i while i is less than 6. As soon as i becomes "not less than 6", then the loop stops. Critically, inside the loop, the value of i increases each iteration.
```{r}
i <- 1 # create an variable
while (i < 6) {
print(i)
i = i+1 #add one eachs step of the loop
}
```
### Repeat loops
Similar to while, but let's do things until a condition is met.
```{r}
i<-0
repeat{
i<-i+1
print(i)
if(i==5){
break
}
}
```
### Examples
Braces are not needed on one line
```{r}
for(i in 1:5) print(i)
```
Using the value of the iterator to assign in values systematically to another variable.
```{r}
# put 1 into the first five positions of x
x <- c() # create empty vector
for(i in 1:5){
x[i] <- 1 # assign 1 to the ith slot in x
}
x
# put the numbers 1-5 in the first 5 positions of x
x <-c()
for(i in 1:5){
x[i] <- i
}
x
```
Make your own counter, when you need one
```{r}
a <- c(1,4,3,5,7,6,8,2)
odd <- c()
counter <- 0
for(i in a){ # i will the values of a in each position
counter <- counter+1
if(i%%2 != 0){
odd[counter] <- "odd"
} else {
odd[counter] <- "even"
}
}
odd
# An alternative strategy
a <- c(1,4,3,5,7,6,8,2)
odd <- c()
# 1:length(a) creates a sequence from 1 to length
for(i in 1:length(a)){
if(a[i]%%2 != 0){
odd[i] <- "odd"
} else {
odd[i] <- "even"
}
}
odd
```
Nesting loops
```{r}
for(i in 1:5){
for(j in 1:5){
print(c(i,j))
}
}
# example of using nested loops to fill the contents
# of a matrix
my_matrix <- matrix(0,ncol=5,nrow=5)
for(i in 1:5){
for(j in 1:5){
my_matrix[i,j] <- i*j
}
}
my_matrix
```
break exits out of the immediate loop
```{r}
# the inside loop stops when i+j is greater than 5
# the outside loop keeps going
sum_of_i_j <- c()
counter <- 0
for(i in 1:5){
for(j in 1:5){
counter <- counter+1
sum_of_i_j[counter] <- i+j
if(i+j > 5){
break
}
}
}
sum_of_i_j
```
## Functions
This section discusses the syntax for writing custom functions in R.
### function syntax
```{r, eval=FALSE}
function_name <- function(input1,input2){
#code here
return(something)
}
```
### example functions
This function has no input between the `()`. Whenever you run this function, it will simply return whatever is placed inside the `return` statement.
```{r}
# define the function
print_hello_world <- function(){
return(print("hello world"))
}
# use the function
print_hello_world()
```
This function simply takes an input, and then returns the input without modifying it.
```{r}
return_input <- function(input){
return(input)
}
# the variable input is assigned a 1
# then we return(input), which will result in a 1
# because the function internally assigns 1 to the input
return_input(1)
a <- "something"
return_input(a)
```
This function takes an input, then creates an internal variable called temp and assigns input+1. Then the contents of temp is returned. Note there, is no checking of the input, so it will return an erro if you input a character (can't add one to a character in R)
```{r, error=TRUE}
add_one <- function(input){
temp <- input+1
return(temp)
}
add_one(1)
add_one("a")
```
This function adds some input checking. We only add one if the input is a numeric type. Otheriwse, we use `stop()` to return an error message to the console
```{r, error=TRUE}
add_one <- function(input){
if(class(input) == "numeric"){
temp <- input+1
return(temp)
} else {
return(stop("input must be numeric"))
}
}
add_one(1)
add_one("a")
```
A function with three inputs
```{r}
add_multiply <- function(input, x_plus,x_times){
temp <- (input+x_plus)*x_times
return(temp)
}
# input is 1
# x_plus <- 2
# x_times <- 3
# will return (1+2)*3 = 9
add_multiply(1,2,3)
```
## Tidyverse
The [tidyverse](https://www.tidyverse.org) is a set of popular R packages that are convenient for many aspects of data-analysis. All of the tidyverse packages can be installed in one go:
```{r, eval=FALSE}
install.packages("tidyverse")
```
---
## dplyr
The `dplyr` package has several useful functions for manipulating and summarizing data.frames. To illustrate some dplyr functionality we first create a small fake data.frame. Here is a link to the [dplyr cheatsheet](https://github.com/rstudio/cheatsheets/raw/master/data-transformation.pdf)
```{r}
subjects <- rep(1:10)
grades <- rnorm(n = 10, mean = 65, sd = 5)
age <- sample(18:20,10,replace=TRUE)
likes_chocolate <- sample(c(TRUE,FALSE), 10, replace=TRUE)
favorite_color <- sample(c("r","o","y","g","b","i","v"), 10, replace=TRUE)
fake_data <- data.frame(subjects,
grades,
age,
likes_chocolate,
favorite_color)
knitr::kable(head(fake_data))
```
### group_by and summarize
`group_by()` allows you to specify columns to split into groups for analysis, these groups are the levels of the column (e.g., unique entries in the column)
`summarize()` conducts an analysis for each group identified in the `group_by` step. The analysis is defined by variable names, and supplying a function that computes a value given the name of a measurement variable.
```{r}
library(dplyr)
fake_data %>%
group_by(likes_chocolate) %>%
summarize(mean_grade = mean(grades),
sd_grad = sd(grades))
fake_data %>%
group_by(likes_chocolate,age) %>%
summarize(mean_grade = mean(grades),
sd_grad = sd(grades))
```
### filter
Filter out rows depending on logical comparisons
```{r}
fake_data %>%
filter(grades < 65)
fake_data %>%
filter(grades < 65,
likes_chocolate == TRUE)
```
### select
Select specific columns
```{r}
fake_data %>%
select(grades)
fake_data %>%
select(grades,likes_chocolate)
```
### mutate
`mutate()` can add a column
```{r}
fake_data <- fake_data %>%
mutate(new_thing = 0)
fake_data
```
## ggplot2
`ggplot2` is a library created by Hadley Wickham for plotting and graphing results, and refers to a "grammar of graphics", or a standardized syntax and organization for graphing.
### ggplot2 additional resources
- <https://ggplot2.tidyverse.org>
- <https://r4ds.had.co.nz/data-visualisation.html>
- <https://ggplot2-book.org>
- <http://r-statistics.co/ggplot2-Tutorial-With-R.html>
- <https://datacarpentry.org/R-ecology-lesson/04-visualization-ggplot2.html>
#### Add-on packages
- [https://www.ggplot2-exts.org](https://www.ggplot2-exts.org) A repository of 50+ add on packages for ggplot2
- [https://gganimate.com](https://gganimate.com)
- allows you to create animated .gifs of ggplots
- I made a bunch of animated gifs for a statistics textbook. They are all here along with the code [https://crumplab.github.io/statistics/gifs.html](https://crumplab.github.io/statistics/gifs.html)
- [ggrepel](https://github.com/slowkow/ggrepel]) allows you to repel overlapping text labels away from each other.
- [esquisse](https://github.com/dreamRs/esquisse) a GUI (graphic user interface) that allows you make ggplot graphs using drag-drop, and clickable options
- [ggedit](https://github.com/metrumresearchgroup/ggedit) similar to above, clickable editing of ggplot graphs
- [plotly](https://plot.ly/r/) a package similar to ggplot, makes a whole variety of graphs, mainly for use in websites. Allows interactive graphs.
- As an example, I used plotly on my publications website, if you hover over the dots, info pops up [https://crumplab.github.io/Publications.html](https://crumplab.github.io/Publications.html).
- **ggpubr** (install from CRAN), many useful things, including the `ggarrange` function which allows you to knit multiple plots together
- **ggthemes** extra themes, scales, and geoms
### Example code
Remember to load the ggplot2 library before you use ggplot2.
```{r}
library(ggplot2)
```
### Scatterplot
```{r}
# Create dataframe
a <- c(1,2,3,2,3,4,5,4)
b <- c(4,3,4,3,2,1,2,3)
plot_df <- data.frame(a,b)
# basic scatterplot
ggplot(plot_df, aes(x=a,y=b))+
geom_point()
# customize, add regression line
ggplot(plot_df, aes(x=a,y=b))+
geom_point(size=2)+
geom_smooth(method=lm)+
coord_cartesian(xlim=c(0,7),ylim=c(0,10))+
xlab("x-axis label")+
ylab("y-axis label")+
ggtitle("I made a scatterplot")+
theme_classic(base_size=12)+
theme(plot.title = element_text(hjust = 0.5))
```
### bar graph
1 factor
```{r}
#Create a dataframe
factor_one <- as.factor(c("A","B","C"))
dv_means <- c(20,30,40)
dv_SEs <- c(4,3.4,4)
plot_df <- data.frame(factor_one,
dv_means,
dv_SEs)
# basic bar graph
ggplot(plot_df, aes(x=factor_one,y=dv_means))+
geom_bar(stat="identity")
# adding error bars, customizing
ggplot(plot_df, aes(x=factor_one,y=dv_means))+
geom_bar(stat="identity")+
geom_errorbar(aes(ymin=dv_means-dv_SEs,
ymax=dv_means+dv_SEs),
width=.2)+
coord_cartesian(ylim=c(0,100))+
xlab("x-axis label")+
ylab("y-axis label")+
ggtitle("I made a bar graph")+
theme_classic(base_size=12)+
theme(plot.title = element_text(hjust = 0.5))
```