Exploratory Data Analysis – Practice Programming Assignment: swirl Lesson 3: Graphics Devices in R

| | 0%

| Graphics_Devices_in_R. (Slides for this and other Data
| Science courses may be found at github
| https://github.com/DataScienceSpecialization/courses/. If
| you care to use them, they must be downloaded as a zip file
| and viewed locally. This lesson corresponds to
| 04_ExploratoryAnalysis/Graphics_Devices_in_R.)

|== | 3%
| As the title suggests, this will be a short lesson
| introducing you to graphics devices in R. So, what IS a
| graphics device?

|=== | 6%
| Would you believe that it is something where you can make a
| plot appear, either a screen device, such as a window on
| your computer, OR a file device?

|===== | 9%
| There are several different kinds of file devices with
| particular characteristics and hence uses. These include
| PDF, PNG, JPEG, SVG, and TIFF. We’ll talk more about these
| later.

|====== | 12%
| To be clear, when you make a plot in R, it has to be “sent”
| to a specific graphics device. Usually this is the screen
| (the default device), especially when you’re doing
| exploratory work. You’ll send your plots to files when
| you’re ready to publish a report, make a presentation, or
| send info to colleagues.

|======== | 15%
| How you access your screen device depends on what computer
| system you’re using. On a Mac the screen device is launched
| with the call quartz(), on Windows you use the call
| windows(), and on Unix/Linux x11(). On a given platform
| (Mac, Windows, Unix/Linux) there is only one screen device,
| and obviously not all graphics devices are available on all
| platforms (i.e. you cannot launch windows() on a Mac).

|========= | 18%
| Run the R command ?Devices to see what graphics devices are
| available on your system.

?Devices

| You are really on a roll!

|=========== | 21%
| R Documentation shows you what’s available.

|============ | 24%
| There are two basic approaches to plotting. The first,
| plotting to the screen, is the most common. It’s simple –
| you call a plotting function like plot, xyplot, or qplot
| (which you call depends on the plotting system you favor,
| but that’s another lesson), so that the plot appears on the
| screen. Then you annotate (add to) the plot if necessary.

|============== | 26%
| As an example, run the R command with with 2 arguments. The
| first is a dataset, faithful, which comes with R, and the
| second is a call to the base plotting function plot. Your
| call to plot should have two arguments, eruptions and
| waiting. Try this now to see what happens.

library(faithful)
Error in library(faithful) : there is no package called ‘faithful’
datasets::faithful
eruptions waiting
1 3.600 79
2 1.800 54
3 3.333 74
4 2.283 62
5 4.533 85
6 2.883 55
7 4.700 88
8 3.600 85
9 1.950 51
10 4.350 85
11 1.833 54
12 3.917 84
13 4.200 78
14 1.750 47
15 4.700 83
16 2.167 52
17 1.750 62
18 4.800 84
19 1.600 52
20 4.250 79
21 1.800 51
22 1.750 47
23 3.450 78
24 3.067 69
25 4.533 74
26 3.600 83
27 1.967 55
28 4.083 76
29 3.850 78
30 4.433 79
31 4.300 73
32 4.467 77
33 3.367 66
34 4.033 80
35 3.833 74
36 2.017 52
37 1.867 48
38 4.833 80
39 1.833 59
40 4.783 90
41 4.350 80
42 1.883 58
43 4.567 84
44 1.750 58
45 4.533 73
46 3.317 83
47 3.833 64
48 2.100 53
49 4.633 82
50 2.000 59
51 4.800 75
52 4.716 90
53 1.833 54
54 4.833 80
55 1.733 54
56 4.883 83
57 3.717 71
58 1.667 64
59 4.567 77
60 4.317 81
61 2.233 59
62 4.500 84
63 1.750 48
64 4.800 82
65 1.817 60
66 4.400 92
67 4.167 78
68 4.700 78
69 2.067 65
70 4.700 73
71 4.033 82
72 1.967 56
73 4.500 79
74 4.000 71
75 1.983 62
76 5.067 76
77 2.017 60
78 4.567 78
79 3.883 76
80 3.600 83
81 4.133 75
82 4.333 82
83 4.100 70
84 2.633 65
85 4.067 73
86 4.933 88
87 3.950 76
88 4.517 80
89 2.167 48
90 4.000 86
91 2.200 60
92 4.333 90
93 1.867 50
94 4.817 78
95 1.833 63
96 4.300 72
97 4.667 84
98 3.750 75
99 1.867 51
100 4.900 82
101 2.483 62
102 4.367 88
103 2.100 49
104 4.500 83
105 4.050 81
106 1.867 47
107 4.700 84
108 1.783 52
109 4.850 86
110 3.683 81
111 4.733 75
112 2.300 59
113 4.900 89
114 4.417 79
115 1.700 59
116 4.633 81
117 2.317 50
118 4.600 85
119 1.817 59
120 4.417 87
121 2.617 53
122 4.067 69
123 4.250 77
124 1.967 56
125 4.600 88
126 3.767 81
127 1.917 45
128 4.500 82
129 2.267 55
130 4.650 90
131 1.867 45
132 4.167 83
133 2.800 56
134 4.333 89
135 1.833 46
136 4.383 82
137 1.883 51
138 4.933 86
139 2.033 53
140 3.733 79
141 4.233 81
142 2.233 60
143 4.533 82
144 4.817 77
145 4.333 76
146 1.983 59
147 4.633 80
148 2.017 49
149 5.100 96
150 1.800 53
151 5.033 77
152 4.000 77
153 2.400 65
154 4.600 81
155 3.567 71
156 4.000 70
157 4.500 81
158 4.083 93
159 1.800 53
160 3.967 89
161 2.200 45
162 4.150 86
163 2.000 58
164 3.833 78
165 3.500 66
166 4.583 76
167 2.367 63
168 5.000 88
169 1.933 52
170 4.617 93
171 1.917 49
172 2.083 57
173 4.583 77
174 3.333 68
175 4.167 81
176 4.333 81
177 4.500 73
178 2.417 50
179 4.000 85
180 4.167 74
181 1.883 55
182 4.583 77
183 4.250 83
184 3.767 83
185 2.033 51
186 4.433 78
187 4.083 84
188 1.833 46
189 4.417 83
190 2.183 55
191 4.800 81
192 1.833 57
193 4.800 76
194 4.100 84
195 3.966 77
196 4.233 81
197 3.500 87
198 4.366 77
199 2.250 51
200 4.667 78
201 2.100 60
202 4.350 82
203 4.133 91
204 1.867 53
205 4.600 78
206 1.783 46
207 4.367 77
208 3.850 84
209 1.933 49
210 4.500 83
211 2.383 71
212 4.700 80
213 1.867 49
214 3.833 75
215 3.417 64
216 4.233 76
217 2.400 53
218 4.800 94
219 2.000 55
220 4.150 76
221 1.867 50
222 4.267 82
223 1.750 54
224 4.483 75
225 4.000 78
226 4.117 79
227 4.083 78
228 4.267 78
229 3.917 70
230 4.550 79
231 4.083 70
232 2.417 54
233 4.183 86
234 2.217 50
235 4.450 90
236 1.883 54
237 1.850 54
238 4.283 77
239 3.950 79
240 2.333 64
241 4.150 75
242 2.350 47
243 4.933 86
244 2.900 63
245 4.583 85
246 3.833 82
247 2.083 57
248 4.367 82
249 2.133 67
250 4.350 74
251 2.200 54
252 4.450 83
253 3.567 73
254 4.500 73
255 4.150 88
256 3.817 80
257 3.917 71
258 4.450 83
259 2.000 56
260 4.283 79
261 4.767 78
262 4.533 84
263 1.850 58
264 4.250 83
265 1.983 43
266 2.250 60
267 4.750 75
268 4.117 81
269 2.150 46
270 4.417 90
271 1.817 46
272 4.467 74

| Not exactly. Give it another go. Or, type info() for more
| options.

| Type with(faithful, plot(eruptions, waiting)) at the
| command prompt.

with(faithful,plot(eruptions,waiting))

| You’re the best!

|=============== | 29%
| See how R created a scatterplot on the screen for you? This
| shows that relationship between eruptions of the geyser Old
| Faithful and waiting time. Now use the R function title
| with the argument main set equal to the string “Old
| Faithful Geyser data”. This is an annotation to the plot.

title(“Old Faithful Geyser data”)

| You’re the best!

|================= | 32%
| Simple, right? Now run the command dev.cur(). This will
| show you the current plotting device, the screen.

dev.cur()
RStudioGD
2

| Perseverance, that’s the answer.

|================== | 35%
| The second way to create a plot is to send it to a file
| device. Depending on the type of plot you’re making, you
| explicitly launch a graphics device, e.g., a pdf file. Type
| the command pdf(file=”myplot.pdf”) to launch the file
| device. This will create the pdf file myplot.pdf in your
| working directory.

pdf(file=

  • “myplot.pdf”)

| That’s the answer I was looking for.

|==================== | 38%
| You then call the plotting function (if you are using a
| file device, no plot will appear on the screen). Run the
| with command again to plot the Old Faithful data. Use the
| up arrow key to recover the command and save yourself some
| typing.

with(faithful,plot(eruptions,waiting))

| Keep up the great work!

|===================== | 41%
| Now rerun the title command and annotate the plot. (Up
| arrow keys are great!)

title(“Old Faithful Geyser data”)

| You are really on a roll!

|======================= | 44%
| Finally, when plotting to a file device, you have to close
| the device with the command dev.off(). This is very
| important! Don’t do it yet, though. After closing, you’ll
| be able to view the pdf file on your computer.

|======================== | 47%
| There are two basic types of file devices, vector and
| bitmap devices. These use different formats and have
| different characteristics. Vector formats are good for line
| drawings and plots with solid colors using a modest number
| of points, while bitmap formats are good for plots with a
| large number of points, natural scenes or web-based plots.

|========================== | 50%
| We’ll mention 4 specific vector formats. The first is pdf,
| which we’ve just used in our example. This is useful for
| line-type graphics and papers. It resizes well, is usually
| portable, but it is not efficient if a plot has many
| objects/points.

|============================ | 53%
| The second is svg which is XML-based, scalable vector
| graphics. This supports animation and interactivity and is
| potentially useful for web-based plots.

|============================= | 56%
| The last two vector formats are win.metafile, a
| Windows-only metafile format, and postscript (ps), an older
| format which also resizes well, is usually portable, and
| can be used to create encapsulated postscript files.
| Unfortunately, Windows systems often don’t have a
| postscript viewer.

|=============================== | 59%
| We’ll also mention 4 different bitmap formats. The first is
| png (Portable Network Graphics) which is good for line
| drawings or images with solid colors. It uses lossless
| compression (like the old GIF format), and most web
| browsers can read this format natively. In addition, png is
| good for plots with many points, but it does not resize
| well.

|================================ | 62%
| In contrast, jpeg files are good for photographs or natural
| scenes. They use lossy compression, so they’re good for
| plots with many points. Files in jpeg format don’t resize
| well, but they can be read by almost any computer and any
| web browser. They’re not great for line drawings.

|================================== | 65%
| The last two bitmap formats are tiff, an older lossless
| compression meta-format and bmp which is a native Windows
| bitmapped format.

|=================================== | 68%
| Although it is possible to open multiple graphics devices
| (screen, file, or both), when viewing multiple plots at
| once, plotting can only occur on one graphics device at a
| time.

|===================================== | 71%
| The currently active graphics device can be found by
| calling dev.cur(). Try it now to see what number is
| assigned to your pdf device.

dev.cur()
pdf
4

| Keep up the great work!

|====================================== | 74%
| Now use dev.off() to close the device.

dev.off()
RStudioGD
2

| All that practice is paying off!

|======================================== | 76%
| Now rerun dev.cur() to see what integer your plotting
| window is assigned.

dev.cur()
RStudioGD
2

| You got it!

|========================================= | 79%
| The device is back to what it was when you started. As you
| might have guessed, every open graphics device is assigned
| an integer greater than or equal to 2. You can change the
| active graphics device with dev.set() where
| is the number associated with the graphics device
| you want to switch to.

|=========================================== | 82%
| You can also copy a plot from one device to another. This
| can save you some time but beware! Copying a plot is not an
| exact operation, so the result may not be identical to the
| original. R provides some functions to help you do this.
| The function dev.copy copies a plot from one device to
| another, and dev.copy2pdf specifically copies a plot to a
| PDF file.

|============================================ | 85%
| Just for fun, rerun the with command again, with(faithful,
| plot(eruptions, waiting)), to plot the Old Faithful data.
| Use the up arrow key to recover the command if you don’t
| feel like typing.

with(faithful,plot(eruptions,waiting))

| Nice work!

|============================================== | 88%
| Now rerun the title command, title(main = “Old Faithful
| Geyser data”), to annotate the plot. (Up arrow keys are
| great!)

title(“Old Faithful Geyser data”)

| All that practice is paying off!

|=============================================== | 91%
| Now run dev.copy with the 2 arguments. The first is png,
| and the second is file set equal to “geyserplot.png”. This
| will copy your screen plot to a png file in your working
| directory which you can view AFTER you close the device.

dev.copy(png,faithful==”geyserplot.png”)
png
4

| That’s not the answer I was looking for, but try again. Or,
| type info() for more options.

| Type dev.copy(png, file = “geyserplot.png”) at the command
| prompt.

dev.copy(png,file==”geyserplot.png”)
Error in file == “geyserplot.png” :
comparison (1) is possible only for atomic and list types
dev.copy(png,file=”geyserplot.png”)
png
5

| Excellent job!

|================================================= | 94%
| Don’t forget to close the PNG device! Do it NOW!!! Then
| you’ll be able to view the file.

getwd()
[1] “C:/Users/Admin/Documents”

| Not exactly. Give it another go. Or, type info() for more
| options.

| Type dev.off() at the command prompt.

dev.off()
RStudioGD
2

| Great job!

|================================================== | 97%
| Congrats! We hope you found this lesson deviced well!

4 thoughts on “Exploratory Data Analysis – Practice Programming Assignment: swirl Lesson 3: Graphics Devices in R

  1. Hi there! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly? My web site looks weird when browsing from my apple iphone. I’m trying to find a template or plugin that might be able to resolve this issue. If you have any suggestions, please share. Cheers!

    Like

    1. Thanks for comment! Do you mean it is weird even if you view it with WordPress app? I am not too firm about the WordPress mobile site so I always use desktop site to work on. I am currently taking the full stack web development course though.

      Like

  2. Its such as you learn my thoughts! You seem to understand a lot approximately this, such as you wrote the ebook in it or something. I believe that you could do with a few percent to drive the message house a little bit, however other than that, that is excellent blog. A fantastic read. I will definitely be back.

    Like

Leave a comment