-
Notifications
You must be signed in to change notification settings - Fork 1
/
io_plots.Rmd
158 lines (110 loc) · 3.26 KB
/
io_plots.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
# How to save plots
## With R Studio
<img src="images/rstudio_plotsave.png" width="450"/>
## With the console
```{r}
# Open the file that will contain your plot (the name is up to you)
pdf(file="myplot.pdf")
# execute the plot
plot(1:10)
# Close and save the file that will contain the plot
dev.off()
```
<h4>Formats</h4>
R supports a variety of file formats for figures: pdf, png, jpeg, tiff, bmp, svg, ps.<br>
They all come with their own function, for example:
```{r}
# TIFF
tiff(file="myfile.tiff")
plot(1:10)
dev.off()
# JPEG
jpeg(file="myfile.jpeg")
plot(1:10)
dev.off()
# etc.
```
The size of the output file can be changed:
```{r}
# Default: 7 inches (both width and height) for svg, pdf, ps.
svg(file="myfile.svg", width=8, height=12)
plot(1:10)
dev.off()
# Default: 480 pixels (both width and height) for jpeg, tiff, png, bmp.
png(file="myfile.png", width=500, height=600)
plot(1:10)
dev.off()
```
*Note that pdf is the only format that supports saving several pages*:
```{r}
pdf(file="myfile_pages.pdf")
plot(1:10)
plot(2:20)
dev.off()
```
* If your file is empty: **run `dev.off()` once or more to make sure there is no devices/files still open!**.
<h4>Plot several figures in one page</h4>
You can output more than one plot per page using the **par()** function (`par()` sets graphical parameters) and the **mfrow** argument.
```{r}
jpeg(file="myfile_multi.jpeg")
# organize the plot in 1 row and 2 columns:
# nr: number of rows
# nc: number of columns
par(mfrow=c(nr=1, nc=2))
plot(1:10)
plot(2:20)
dev.off()
```
<img src="images/plots/myfile_multi.jpeg" width="350/" >
```{r}
jpeg(file="myfile_multi4.jpeg")
# organize the plot in 2 rows and 2 columns
par(mfrow=c(nr=2, nc=2))
# top-left
plot(1:10)
# top-right
barplot(table(rep(c("A","B"), c(2,3))))
# bottom-left
pie(table(rep(c("A","B"), c(2,3))))
# bottom-right
hist(rnorm(2000))
dev.off()
```
<img src="images/plots/myfile_multi4.jpeg" width="500/" >
**HANDS-ON**
Go back to the previous plots you created (if you didn't save commands in an R script, you can refer to the *History* tab in the top-right panel):
* Save 1 plot of your choice in a **jpeg** file.
* Save 3 plots of your choice in a **pdf** file (one plot per page).
* Organize the same 3 plots in 1 row / 3 columns. Save the image in a **png** file. Play with the **width** (and perhaps also **height**) argument of `png()` until you are satisfied with the way the plot renders.
<details>
<summary>
*Answer*
</summary>
```{r}
# Save 1 plot of your choice in a **jpeg** file.
jpeg("myboxplot.jpeg")
boxplot(chickwts$weight ~ chickwts$feed, las=2)
dev.off()
# Save 3 plots of your choice in a **pdf** file (one plot per page).
pdf("my3plots.pdf")
boxplot(chickwts$weight ~ chickwts$feed, las=2)
barplot(table(chickwts$feed))
plot(x=Loblolly$age, y=Loblolly$height,
main="Age and weight of Loblolly pine trees",
pch=17,
col="red",
cex=0.4)
dev.off()
# Organize the same 3 plots in 1 row / 3 columns. Save the image in a **png** file.
png("my3plots.png", width=1000, height=350)
par(mfrow=c(1, 3))
boxplot(chickwts$weight ~ chickwts$feed, las=2)
barplot(table(chickwts$feed))
plot(x=Loblolly$age, y=Loblolly$height,
main="Age and weight of Loblolly pine trees",
pch=17,
col="red",
cex=0.4)
dev.off()
```
</details>