-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule_09_Plotting.Rmd
159 lines (111 loc) · 3.91 KB
/
Module_09_Plotting.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
---
title: 'Module 9: Plotting'
author: "Brian P Steves"
date: "`r Sys.Date()`"
output: html_document
---
## Creating Plots
### Base Plot Functions
#### plot()
The plot() function is a generic function for plotting objects in R. The most basic form of plot() is plotting coordinates X and Y.
```{r basic_plot}
# basic x,y plot
xdat <- seq(1,10,by=0.1)
ydat <- sin(xdat)
# using x,y
plot(xdat, ydat)
#using y~x formula
plot(ydat~xdat)
```
#### lines()
You can add a line connecting points on a plot using lines().
```{r basic_plot_with_lines}
plot(xdat,ydat)
lines(xdat,ydat)
```
#### barplot()
Barplot() plots a bar plot.
```{r barplot}
# loading some builtin data
print(VADeaths)
barplot(VADeaths)
barplot(VADeaths, beside=TRUE)
```
#### hist()
To plot histograms you can use the hist() function.
```{r histogram}
# 1000 normally random points with a mean of 0 and a SD of 1
x <- rnorm(1000)
hist(x)
```
You can specify the number of breaks.
```{r histogram2}
hist(x,breaks = 5)
hist(x, breaks = 50)
```
### plot() with specific R objects
Sometimes using plot() with an object generated by specific functions will create very specific type of plot.
For example, plotting the outpu of a hierarchical clustering (hclust).
```{r hcluster}
hc <- hclust(dist(USArrests), "ave")
plot(hc)
```
### Refining plots with par
If you want to tweak basic plot functions you need to familarize your self with par (i.e. graphic parameters). These graphical parameters can be set within the plot function or afterwards with the par() function. (see ?par for more information).
Some basic plotting parameters..
pch = an integer specifying the symbol to be used in plotting. For example, 1 is an open circle, 3 is a plus, 16 is a filled circle, etc.. (see ?points) for more details.
col = to specify a color
cex = specify the magnification of symbol (and other features) size.
```{r basic_with_par}
# plot x vs y again in red, filled circles, twice normal size
plot(xdat,ydat, col="red", pch=16, cex=2)
```
### Titles and axis labels
```{r basic_with_labels}
plot(xdat,ydat, main="Sine curve",xlab="X Values",ylab="Y Values")
```
### ggplot2 package
The ggplot2 package introduces an entirely different way to plot using the "grammar of graphics". The general principle is that plots (graphics) are created with a series of building blocks each consisting of data, mapping of aesthetics, geometric objects, and potentially a statistical transformation.
For detailed documentation see the following site...
http://docs.ggplot2.org/current/
Here's a simple example..
```{r ggplot_sine_curve}
library(ggplot2)
dat <- data.frame(xdat, ydat)
p <- ggplot(dat, aes(x=xdat, y=ydat)) + geom_line()
print(p)
```
Here's a more complicated example that based on more complicated data.
```{r ggplot_diamonds}
# some built in data about diamonds
head(diamonds)
p <- ggplot(diamonds)
# plot carat vs price
p + geom_point(aes(x=carat, y=price))
# plot carat vs price and color by clarity
p + geom_point(aes(x=carat, y=price, colour = clarity))
# plot carat vs price, color by clarity and set the transparancy to 0.5
p + geom_point(aes(x=carat, y=price, colour = clarity), alpha = 0.5)
```
Here's another example. We set up the x and y aesthetic within the main ggplot object and the subsequent geom_point() and geom_line() objects end up using that same x,y
```{r ggplot_smooth}
# using some data about cars
head(mpg)
p <- ggplot(mpg, aes(x=displ, y=hwy)) + geom_point() + geom_smooth(method=lm)
p
```
Multiple plots using facet_wrap()
```{r ggplot_facet_wrap}
p <- ggplot(mpg, aes(x=displ, y=hwy))
p <- p + geom_point(aes(col=class)) + facet_wrap(~manufacturer)
p
```
The overall style of the a ggplot2 plot can be changed with themes
```{r}
p + theme_bw()
```
There are several preset themes and you can even create your own.
A ggplot2 cheat sheet
https://rstudio.github.io/cheatsheets/data-visualization.pdf
ggplot2 documentation site
https://ggplot2.tidyverse.org/