Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions Reproduce steps 1-10 in Section 7.3 - ADDING POINTS, LINES, AND TEXT TO AN EXISTING PLOT of your textbook to generate the plot shown

Instructions

Reproduce steps 1-10 in "Section 7.3 - ADDING POINTS, LINES, AND TEXT TO AN EXISTING PLOT" of your textbook to generate the plot shown in Figure 7.6: An elaborate final plot of some hypothetical data.

Note: Steps 1-10 appear below Figure 7.6 in the textbook

Submission

Submit a single R script containing all 10 steps that can be executed to generate the plot. Annotate your script using R comments to provide descriptive information for the steps that are performed. Include an ID header.

Include the creation of the vectors x and y in your solution, here are those vectors (as created on pg 134 of the text):

x

Note: Your plot will probably not look exactly like Figure 7.6. One change I made to get it closer to the figure was to apply an overall cex scaling value of .75 to the legend.

Instructions

Reproduce steps 1-10 in "Section 7.3 - ADDING POINTS, LINES, AND TEXT TO AN EXISTING PLOT" of your textbook to generate the plot shown in Figure 7.6: An elaborate final plot of some hypothetical data.

Note: Steps 1-10 appear below Figure 7.6 in the textbook

Submission

Submit a single R script containing all 10 steps that can be executed to generate the plot. Annotate your script using R comments to provide descriptive information for the steps that are performed. Include an ID header.

Include the creation of the vectors x and y in your solution, here are those vectors (as created on pg 134 of the text):

x

Note: Your plot will probably not look exactly like Figure 7.6. One change I made to get it closer to the figure was to apply an overall cex scaling value of .75 to the legend.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

In Figure 7-6, the data points will be plotted differently according to their x and y locations, depending on their relation to the "sweet spot" pointed out a the figure. Points with a y value greater than 5 are marked with a purple x; points with a y value less than 5 are marked with a green +. Points between hese two y values but still outside of the sweet spot are marked with a 0 . Finally, points in the sweet spot (with x between 5 and 15 and with y between - 5 nd 5) are marked as a blue . Red horizontal and vertical lines delineate the sweet spot, which is labeled with an arrow, and there's also a legend. Ten lines of code were used to build this plot in its entirety (plus one additional line to add the legend). The plot, as it looks at each step, is given in igure 7-7. The lines of code are detailed next. 1. The first step is to create the empty plotting region where you can add points and draw lines. This first line tells R to plot the data in x and y, though the option type is set to n. As mentioned in Section 7.2, this opens or refreshes the graphics device and sets the axes to the appropriate lengths (with labels and axes), but it doesn't plot any points or lines. Rs plot (x,y,type=n,main=) 2. The abl ine function is a simple way to add straight lines spanning a plot. The line (or lines) can be specified with slope and intercept values (see the later discussions on regression in Chapter 20). You can also simply add horizontal or vertical lines. This line of code adds two separate horizontal lines, one at y=5 and the other at y=5, using h=c (5,5). The three parameters (covered in Section 7.2) make these two lines red, dashed, and double-thickness. For vertical lines, you could have written v=c(5,5), which would have drawn them at x=5 and x=5. (9) (10) Figure 7-7: Building the final plot given in Figure 7-6. The plots (1) through (10) correspond to the itemized lines of code in the text. R> abline (h=c(5,5),col= red* ,1ty=2,1wd=2) The third line of code adds shorter vertical lines between the horizontal ones drawn in step 2 to form a box. For this you use segments, not abiline, since you don't want these lines to span the entire plotting region. 'The segments command takes a "from" coordinate (given as x0 and y 0 ) and a "to" coordinate (as x1 and y1 ) and draws the corresponding line. 'The vector-oriented behavior of R matches up the two sets of "from" and "to" coordinates. Both lines are red and dotted and have double-thickness. (You could also supply vectors of length 2 to these parameters, in which case the first segment would use the first parameter value and the second segment would use the second value.) R> segments (x0=c(5,15),y0=c(5,5),x1=e(5,15),y1=e(5,5), col= red, 1ty=3, 1wd=21 4. As step 4, you use points to begin adding specific coordinates from x and y to the plot. Just like plot, points takes two vectors of equal lengths with x and y values. In this case, you want points plotted differently according to their location, so you use logical vector subsetting (see Section 4.1.5 ) to identify and extract elements of x and y where the y value is greater than or equal to 5 . These (and only these) points are added as purple x symbols and are enlarged by a factor of 2 with cex. R> points (x[y>=5],y[y>=5],pch=4, col = darkmagenta, cex =2) 5. The fifth line of code is much like the fourth; this time it extracts the coordinates where y values are less than or equal to 5.A+ point character is used, and you set the color to dark green. R> points (x[y5ypoints(x[(x>=5x5y=5sx5sy points (x[(x15)&(y>5sy15)&(y>5y > ines (x,y,1ty=4) 9. The ninth line of code adds the arrow pointing to the sweet spot. The function arrows is used just like segments, where you provide a "from" coordinate (x0,y0) and a "to" coordinate (x1,y1). By default, the head of the arrow is located at the "to" coordinate, though this (and other options such as the angle and length of the head) can be altered using optional arguments described in the help file ?arrows. 10. The tenth line prints a label on the plot at the top of the arrow. As per the default behavior of text, the string supplied as labels is centered on the coordinates provided with the arguments x and y. R> cext(x=8,y=15,1abe1s= "sweet spot* ) As a finishing touch, you can add the legend with the legend function, which gives you the final product shown in Figure 7-6. The first argument sets where the legend should be placed. There are various ways to do this (including setting exact x - and y-coordinates), but it often suffices to pick a corner using one of the four following character strings: "topleft ", "topright", "bottomleft", or "bottomright" Next you supply the labels as a vector of character strings to the legend argument. Then you need to supply the remaining argument values in vectors of the same length so that the right elements match up with each label. For example, for the first label (*overa11 process*), you want a line of type 4 with default thickness and color. So, in the first positions of the remaining argument vectors, you set peh=NA, 1ty=4,co1=bblack, , 1wd =1, and pt. cex =NA (all of these are default values, except for 1ty ). Here, pt. cex simply refers to the cex parameter when calling points (using just cex in legend would expand the text used, not the points). Note that you have to fill in some elements in these vectors with NA when you don't want to set the corresponding graphical parameter. This is just to preserve the equal lengths of the vectors supplied so Rcantrackwhichparametervaluescorrespondtoeachparticularreference.Asyouworkthroughthis book, you'll see plenty more examples using legend

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Would it be feasible for SKI to finance with commercial paper?

Answered: 1 week ago