#Question Study the lecture notes and screen cast for (Base) Graphics, Part 1: Overview and common parameters (2:20). Look at the help for points() via ?points. Scroll half way down to see the chart that shows the numeric codes for 26 plotting characters (pch) corresponding to each code. Which pch corresponds to a solid triangle? (Note that the solid triangle comes after two unfilled triangles and before two filled triangles.)
?points # 17 for the filled triangle
#Question Study Part 2: Numeric data: boxplot and stripchart (4:59). Consider the built-in mtcars data frame. (Run mtcars and ?mtcars.) Make a grouped boxplot of mpg, grouping by gear. For which number of gears is the interquartile range (IQR, the height of the box) of mpg the largest?
boxplot(mpg ~ factor(gear), data = mtcars)
#Answer is 5 gear
#Question 3. Consider the built-in iris data frame. (See ?iris.) Make a grouped boxplot of Petal.Length, grouping by Species. For which species is there no outlier? Answer with a digit via 1=setosa 2=versicolor, 3=virginica.
boxplot(Petal.Length ~factor(Species), data = iris)
# or can also declare own dataframe and use
i = iris
boxplot(i$Petal.Length ~factor(Species), data = i)
#Question 4. Study Part 3: Numeric data: hist, plot, points, and lines (7:34). Make a histogram of Petal.Length from iris. Use the integers from 0 to 7 as breaks. The tallest bar is just to the right of which integer?
hist(iris$Petal.Length, breaks = seq(0,7,1))
#gets it as Petal.Length, and breaks it down from 0 to 7, by integers (ones, 1)
#Question 5. Study Part 4: Numeric data: density, pairs, curve (8:55). Make a density plot of Petal.Length from iris. How many peaks does it show?
plot(density(iris$Petal.Length))
#Question 6. Make a matrix of scatterplots of all pairs of variables in the first four columns of iris. (The fifth column, Species, is categorical and doesn't belong in a scatterplot.) The variable Petal.Length appears to be most strongly correlated with which other variable? Answer with a digit via 1=Sepal.Length, 2=Sepal.Width, 3=Petal.Width.
pairs(iris)
#Answer is petal.Width, there is dark positive correlation
#Question 7. Study Part 5: legend (7:04). How is "a≈b" written in plotmath notation? Answer with a digit via 1=a %approx% b, 2=a approx b, 3=a %~~% b, 4=a ~~ b.
?plotmath #using plotmath, you can see x %~~% y x is approximately equal to y
#Question 8. Study Part 6: Categorical data: barplot and mosaicplot (3:38). Make a barplot of feed from the chickwts data frame (See chickwts.) Which feed is most frequent in these data? Answer with a digit via 1=casein, 2=horsebean, 3=linseed, 4=eatmeal, 5=soybean, 6=sunflower
barplot(table(chickwts$feed))
# Answer is 5, as the soybean is tallest, at 14, of the six types
#Question 9. Study Part 7: Multiple figures via layout (5:37). Show the layout that corresponds to this matrix: matrix(data=c(1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2), nrow=4, ncol=4, byrow=TRUE). How many figures in one graph does this layout support?
ma = matrix(data=c(1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2), nrow=4, ncol=4, byrow=TRUE)
layout(ma)
layout.show(2)
# The answer is 2, because there are only two types of data, 1 and 2, in this matrix
#Question 10. Study Part 8: Write a graph to a file: pdf, png, ..., dev.off (1:46). Make a graph, saving it to a file. View that file (outside of RStudio). Did it work? Answer with a digit via 1=yes, 2=no.
pdf("junk.pdf") # This one opens a pdf file with the name "junk.pdf"
x = rnorm(100) # This line sets x equal to 100 random points on the normal curve
plot(density(x)) # THis line plots a density graph of x
dev.off() # This line closes off the pdf file
#Automating this is very cool!
#The answer is 1, because I can save the graph into a pdf!
LS0tDQp0aXRsZTogIlF1aXogNSBTY3JpcHRzIg0Kb3V0cHV0OiBodG1sX25vdGVib29rDQotLS0NCg0KYGBge3J9DQojUXVlc3Rpb24gU3R1ZHkgdGhlIGxlY3R1cmUgbm90ZXMgYW5kIHNjcmVlbiBjYXN0IGZvciAoQmFzZSkgR3JhcGhpY3MsIFBhcnQgMTogT3ZlcnZpZXcgYW5kIGNvbW1vbiBwYXJhbWV0ZXJzICgyOjIwKS4gTG9vayBhdCB0aGUgaGVscCBmb3IgcG9pbnRzKCkgdmlhID9wb2ludHMuIFNjcm9sbCBoYWxmIHdheSBkb3duIHRvIHNlZSB0aGUgY2hhcnQgdGhhdCBzaG93cyB0aGUgbnVtZXJpYyBjb2RlcyBmb3IgMjYgcGxvdHRpbmcgY2hhcmFjdGVycyAocGNoKSBjb3JyZXNwb25kaW5nIHRvIGVhY2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