Description
You need package ‘ggplot2’: install.packages(“ggplot2”). Two datasets that are shipped with the ggplot2 package: diamonds: describing the prices of more than 50,000 cut diamonds and msleep describing the sleep times of 83 mammals. Then,Create a scatterplot with carat along the x-axis and price along the y-axis. Change the x-axis label to read “Weight of the diamond (carat)” and the y-axis label to “Price (USD).” Use cut to set the color of the points.Try adding the argument alpha = 1 to geom_point, i.e. geom_point(alpha = 1). Does anything happen? Try changing the 1 to 0.5 and 0.25 and see how that affects the plot.Change the scatterplot from above, so that diamonds with different cut qualities are represented by different shapes.Change the scatterplot from above, so that the size of each point is determined by the diamond’s length, i.e. the variable x.
Unformatted Attachment Preview
R Programming
Week 2.2: The R language – “Paragraphs” and “Essays”
Writing scripts
Control of execution flow
if statements
loops
iterations
Writing scripts
Script – a text file that contains commands that you would type at the console
prompt:
o The file contains valid R statements (including comments) and nothing else
o Comments start at a # and end at the end of the line
o The R statements are in the file in the order that they must be executed
o R scripts have file names ending in .r or .R
o script can be “sourced” using function source()
my.first.script.r file:
# this is my first R script
print(3 + 4)
source the file:
source(“my.first.script.r”)
## [1] 7
Writing scripts
By now you should be familiar enough with R to be able to write your own script:
1. Create a new script (from the File menu, “+” icon, or by typing “Ctrl + Shift + N”).
2. Save the file as “my.second.script.r”.
3. Use the editor pane in RStudio to type some R commands and comments.
4. Run individual commands.
5. Source the whole file.
How does one achieve an understandable script or program?
1. Avoid the unusual. As a minimum try to be consistent with yourself.
2. Use meaningful names for variables, and any other object.
3. Traditionally in R one uses dots to separate the words and use only lower case.
Writing scripts
Diagnosing the source of bugs is (like detective work):
o hunches based on common sense and experience
o follow different leads until the case is solved
o rely on some sort of divide-and-conquer strategy
Special tools, called debuggers:
o allow one to step through the code, executing one statement at a time
o R debugger is available within RStudio
o it is wise to very carefully read the documentation
Control of execution flow
Control of execution statements allows flexibility
Individual statements can be grouped into compound statements with curly braces
print(“A”)
## [1] “A”
{
print(“B”)
print(“C”)
}
## [1] “B”
## [1] “C”
if, else
flag
Purchase answer to see full
attachment