Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use this R code: #Questions: Plot from 9. # after changing the point shape to 16 and the color to red # after changing the

Use this R code:

#Questions: Plot from 9. # after changing the point shape to 16 and the color to red # after changing the contour line size (thickness) to 1.5 # Note that functions arguments must be separated by a comma

#hw.R

hw <- theme_gray()+ theme( plot.title=element_text(hjust=0.5), plot.subtitle=element_text(hjust=0.5), plot.caption=element_text(hjust=-.5),

strip.text.y = element_blank(), strip.background=element_rect(fill=rgb(.9,.95,1), colour=gray(.5), size=.2),

panel.border=element_rect(fill=FALSE,colour=gray(.70)), panel.grid.minor.y = element_blank(), panel.grid.minor.x = element_blank(), panel.spacing.x = unit(0.10,"cm"), panel.spacing.y = unit(0.05,"cm"),

# axis.ticks.y= element_blank() axis.ticks=element_blank(), axis.text=element_text(colour="black"), axis.text.y=element_text(margin=margin(0,3,0,3)), axis.text.x=element_text(margin=margin(-1,0,3,0)) )

# ===============================================

#Main file

# Addresses # head(), summary() # library() # The help system # ggplot # aesthetics: x and y position, # shape, size, fill and color # geometric objects # geom_point, geom_smooth, # geom_histogram, geom_line, # geom_freqpoly, geom_density. # statistical calculations: stat_density2d() # superposing geoms # adding labels with labs() # controlling plot themes such as # grid lines # Kernel versus histogram density plots # A bivariate density contour plot

# Sections # 0. Setup # 1. The old faithful geyser data # 2. The R help system # 3. Producing a scatterplot # Adding a smooth to suggest # a functional relationship # 4. Plotting labels # 5. Themes that control plotting # 6. Univariate data density # Kernel versus histogram density plots # Save plots and modifyiny them

#==================================================

# 0. Setup

# uncomment and run if tidyverse is not yet installed #install.packages("tidyverse")

library(tidyverse)

source('hw.R') # Instructor provide ggplot theme

library(MASS) # This has the faithful data set

# 1. The old faithful geyser data

# The data set, faithful, is in the MASS package. # The MASS package comes with R # so does not need to be installed. # # The MASS package still needs to be put # on R's search vector to provide access # the data. The library(MASS)function # has adds the MASS package "folder" to the # second position in R's search list.

search()

faithful

View(faithful)

# 1.2 Quick partial views #

head(faithful) tail(faithful) tail(faithful,10)

# 1.3 A tibble variation of a data.frame # indicates the type variable in each # column and limits the number of rows and # columns shown. This is helpful for large # datasets.

faithful2 <- as_tibble(faithful) faithful2

# 1.4 We can often get information # about R provide datasets # by using ? as a prefix. # with ?

?faithful

# 1.5 We can get descriptive statistics for # variables in data.frames, tibbles, and # other data structures

summary(faithful) summary(faithful2)

# 2. The R help system for functions

?summary

?head

?seq

# try the first and third examples

seq(0, 1, length.out = 11) seq(0, 1, by=.2)

# 3. Producing a scatterplot using ggplot

ggplot(faithful2,aes(x=waiting,y=eruptions))+ geom_point()

# 4. The ggplot() function development # followed the design and terminology # described in Leland Wilkinson's illuminating # book, "The Grammar of Graphics". # # In the book, the term aesthetics refers to # the Greek word that means perception. # In the syntax above,the aes() function # pairs aesthetic key words to tibble variables. # # The most commonly used aesthetics are # x and y position, size, shape, fill and color. # ggplot uses these aesthetics when rendering # geometric objects that we can see. # # Functions with prefix "geom_" specify # geometric objects. # Three examples are geom_point(), geom_line(), and # geom_polygon(). # # We use geom function arguments # to change aesthetic constants.

ggplot(faithful2,aes(x=waiting,y=eruptions))+ geom_point(shape=21, size=3.7, fill="cyan", color="cyan")

ggplot(faithful,aes(x=waiting,y=eruptions)) + geom_point() + geom_smooth(method=loess, color="blue",size=.5)

# ggplot2 supports computing with stat_functions # and plotting the results. For geom_smooth(), # stat_smooth() is the default and vice versa.

ggplot(faithful,aes(x=waiting,y=eruptions)) + geom_point() + stat_smooth(color="purple",size=.8)

# We can choose other data fitting methods. # Below lm, specifies a linear regression model. # Below we also change the geom layer plotting # order and aesthetic value constants.

ggplot(faithful,aes(x=waiting,y=eruptions)) + geom_smooth(method=lm, color="red",size=1.1)+ geom_point()

# Now black points appear on top of the red line.

# 6. Plot labeling

# In this class, graphics are to include variable names # and the units of measure. The units of measure may # appear in variable labels or perhaps in the title. # # When the graphics are in the class project report, the # labeling can appear in figure caption.

ggplot(faithful,aes(x=waiting,y=eruptions)) + geom_point(shape=21, size=2, fill="green", color="black")+ geom_smooth(method=loess, size=1.2) + labs(x="Waiting Time Between Eruptions in Minutes", y="Eruption Duration in Minutes", title="Old Faithful Geyser Eruptions")

ggplot(faithful,aes(x=waiting,y=eruptions)) + geom_point(shape=21, size=2.,fill="green",color="black")+ geom_smooth(method=loess,size=1.2, span=.6)+ labs(x="Waiting Time Between Eruptions In Minutes", y="Eruption Duration in Minutes", title="Old Faithful Geyser Eruptions")

# 7. Themes that control plot appearance

ggplot(faithful,aes(x=waiting,y=eruptions)) + geom_point(shape=21, size=2, fill="green", color="black")+ geom_smooth(method=loess, size=1.2, span=.6) + labs(x="Waiting Time Between Eruptions in Minutes", y="Eruption Duration in Minutes", title="Old Faithful Geyser Eruptions") + hw

# 8. Kernel versus histogram density plots

ggplot(faithful,aes(x=waiting)) + geom_histogram()+ labs(x="Waiting Time Between Eruptions In Minutes", y="Density", title="Old Faithful Geyser Eruptions") + hw

# We can control the binwidth,the # rectangle fill color and the line color.

ggplot(faithful,aes(x=waiting)) + geom_histogram(binwidth=2, fill="cornsilk",color="black")+ labs(x="Waiting Time Between Eruptions In Minutes", y="Counts", title="Old Faithful Geyser Eruptions")+hw

ggplot(faithful,aes(x=waiting,..density..)) + geom_histogram(binwidth=2, fill="cornsilk",color="black")+ labs(x="Waiting Time Between Eruptions In Minutes", y="Density", title="Old Faithful Geyser Eruptions") + hw

histPlot <- ggplot(faithful,aes(x=waiting,..density..))+ geom_histogram(binwidth=2, fill="cornsilk",color="black")+ labs(x="Waiting Time Between Eruptions in Minutes", y="Density", title="Old Faithful Geyser Eruptions")+hw

# Plot it histPlot

# Add a frequency polygon and plot histPlot+ geom_freqpoly(binwidth=2,color="red",size=1.2)

# Add a computed kernel density, extend the x axis and plot histPlot+ geom_line(stat="density",color="blue",size=1.2)+ xlim(33,104)

histPlot + geom_density(adjust=.4,fill="cyan",color="black",alpha=.4)+ xlim(35,102)

# 9. Bivariate Density, a contour example

#Question: Plot from 9. # after changing the point shape to 16 and the color to red # after changing the contour line size (thickness) to 1.5 # Note that functions arguments must be separated by a comma. Please show output graphs

ggplot(faithful,aes(x=waiting,y=eruptions))+ geom_point() + stat_density2d(aes(color=..level..)) + labs(x="Waiting Time Between Eruptions in Minutes", y="Eruption Duration in Minutes", title=paste("Old Faithful Geyser Eruptions:", "Bivariate Density"))+ hw

# Note the grey level and color saturation are poor # encoded for a continuous variable. This will be # discussed later in class.

# A reference above indicated there are two major # underground chambers at different depths.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

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

Business Law With UCC Applications

Authors: Gordon Brown, Paul Sukys

13th Edition

0073524956, 978-0073524955

Students also viewed these Mathematics questions

Question

=+42, develop and compare the following models.

Answered: 1 week ago

Question

How does selection differ from recruitment ?

Answered: 1 week ago