Menu

[Solved]Installpackages Ggpplot2 Library Ggplot2 Library Datasets Use Project Project Initial Setu Q37202075

#install.packages(“ggpplot2”)
library(ggplot2)
library(datasets)

## How to use this project: ##

# In this project, some initial setup has been done and datafiles have been included.
# This helps you avoid running into roadblocks.
# Use this project as a starting point.

# This code is designed to be used as a workbook tutorial ofsorts.
# This ‘code workbook’ is designed so you can carefully study thematerial, as well as write and
# test your code in it. The places where you are expected to writecode are marked as shown below:

# There are 17 assignments in this course

############ ASSIGNMENT # xyz #############

# Assignment#xyz description

#*****your code here******#

# This R script is provided as a starting point. It is designedfor you to read through it carefully
# and use it as a starting point for tweaking code to do thedesired analysis.

# Throughout the code, there are points where you are expectedto insert your own code in the file.
# You can check whether the code you wrote works properly byexecuting your snippet of code.
# To execute a snippet of code, select that part of the code andthen click on Run (The green rightward
# pointing arrow at the top of this code window). When you click onthe “Run” button, the portion of the
# code you selected, gets executed.

# If you wish to execute the whole script from start to finish,select the whole code (make sure that
# your cursor is in the script window and then press ‘ctrl+A’) ,and then click on the Run button above.

# You can see the results of the execution of code in theconsole below (bottom left panel).
# Make sure your code is executing as desired.
# Continue to save your code frequently as you are writting it. Itdoes not save automatically
# (unlike in Google docs).

#************************************************************************************
#A########### TUTORIAL + Assignments on Data Frames#############
#************************************************************************************

## Creating a data frame by reading a .csv file
# Text files are a popular way to hold and exchange tabular data asalmost any data application supports exporting
# data to the CSV (or other text file) formats. Text file formatsuse delimiters to separate the different elements
# in a line, and each line of data is in its own line in the textfile. Therefore, importing different kinds of text
# files can follow a fairly consistent process once you’veidentified the delimiter.

### Importing data into a data frame using read.csv()

# We will now import a real data set into an R dataframe. Thedata we will use contains
# information on the values of residential properties in the UnitedStates. It contains the following information:
# values and price indexes for all land, structures, and housing byregion and state. It also contains home and land
# price indices.

# The data is available in a csv file – ‘landdata-states.csv’.This file is stored in this script under the Files tab in
# the bottom right panel.

# To import the data, we use the read.csv() function as shownbelow:

housing = read.csv(“landdata-states.csv”)

############ ASSIGNMENT # df1 #############

#*Q* Write code here to check whether the data was loaded fromthe csv file, use the summary() and head()
# functions of R:

#*****your code here******#

############ ASSIGNMENT # df2 #############

#*Q* Write code here to check the type of the data in housing.Use the class() function.

#*****your code here******#

#You have successfully imported a csv file into a dataframe in R.This data is now available for you to
# use for all kinds of analysis.

#************************************************************************************
#A########### TUTORIAL + Assignments Using Data set how_we_met.csv#############
#************************************************************************************

# The code below loads ‘how_we_met.csv’ data (find it in the Filestab in bottom right panel of Rstudio)
# file into a dataframe called meeting

meeting <- read.csv(“how_we_met.csv”)

############ ASSIGNMENT # 1 #############

#*Q* Write code here to:
# a) print summary of HowMet
# b) print the top few lines of HowMet
# c) print the class of the dataset HowMet

#*****your code here******#

# In the following code, we use the ggplot2 package to print a lineplot from the data set.

ggplot(meeting) +
geom_line(aes(x = year,y = p, group = waymet, color = waymet))

############ ASSIGNMENT # 2 #############
#*Q* Write code below with the color aesthetic removed from codeabove and re-run. What happens?

#*****your code here******#

# Next we use the facet function of ggplot2 package to create agrid of plots.

ggplot(meeting) +
geom_line(aes(x = year,y = p, group = waymet, color = waymet))+
facet_wrap(~ waymet, ncol = 5)

############ ASSIGNMENT # 3 #############
#*Q* Modify code above to change the number of columns in the gridto 3. Write the code below.

#*****your code here******#

#************************************************************************************
#B########### TUTORIAL + Assignments Using Data setlanddata-states.csv #############
#************************************************************************************

# The code below loads ‘landdata-states.csv’ data (find it inthe Files tab in bottom right panel of Rstudio)
# file into a dataframe called housing

housing <- read.csv(“landdata-states.csv”)

#*Q* Write code here to:
# a) print summary of housing
# b) print the top few lines of housing
# c) print the class of the dataset housing

#*****your code here******#

# In the following code, we use the ggplot2 package to print a lineplot showing land prices over the years.

# First plot: To ensure everything works.
housePlot <- ggplot(housing, aes(x=Home.Value)) +geom_histogram()
ggsave(“housing_hist.pdf”, height=11, width=17, units=’in’) # willstore in your Files (check in bottom right panel of Rstudio underFiles)

# Second plot: Change geometry to line.
ggplot(housing,
aes(x=Date,
y=Home.Value,
color=State))+
geom_line()

ggsave(“housing_line.pdf”, height=11, width=17, units=’in’)

# Once Again you will notice that ggsave has created a PDF filecalled housing_line.pdf in your
# Files.

############ ASSIGNMENT # 4 #############
#*Q* Modify the above code by changing the geometry from geom_lineto geom_point and re-run. Also,
# remove the ggsave line of code to prevent overwriting youralready generated housing_line.pdf file.
# Which geometry is more intuitive in this plot – line orpoint?
# Now add back the ggsave line and save the plot into a PDF filetitled housing_point.pdf

#*****your code here******#

# Now, we are going to learn a very useful and powerful functionused in dataframes – subset.
# Subset does what it sounds like – it creates a subset of the databased on specified criteria.
# Thus subset(housing, State %in% c(“MA”, “TX”, “CA”, “PA”, “MI”))will create a subset of the
# data which only contains data on the above five states.

# We do exactly that in the code below by subsetting to fivestates.
ggplot(subset(housing, State %in% c(“MA”, “TX”, “CA”, “PA”,”MI”)),
aes(x=Date,
y=Home.Value,
color=State))+
geom_line()

ggsave(“housing_line_5States_1.pdf”, height=11, width=17,units=’in’)

############ ASSIGNMENT # 5 #############
#*Q* Modify the above code to subset data for the states of OH, NY,FL, CO and WI and re-run. Also,
# change the ggsave line of code to store it inhousing_line_5States_2.pdf file.

#*****your code here******#

############ ASSIGNMENT # 6 #############
#*Q* Repeat the same for five states of your choice and re-run.Also,
# change the ggsave line of code to store it inhousing_line_My5States.pdf file.

#*****your code here******#

############ ASSIGNMENT # 7 #############
#*Q* Now change the code to subset the data to the Midwestregion.
# Also, change the ggsave line of code to store it inhousing_line_midwest.pdf file.
# Hint: There are different ways to set the conditions forsubsetting.
# For example in the example above we used the %in% operator -State %in% c(“MA”, “TX”, “CA”, “PA”, “MI”)
# The other logical operators are:
# < – less than
# <= –   less than or equal to
# >   – greater than
# >=   – greater than or equal to
# ==   – exactly equal to
# !=   – not equal to
# !x   – Not x
# x | y   – x OR y
# x & y   – x AND y
# %in% c() – in the values within c().
# Thus to subset data to a specific region, the condition to usewill be- region == “region name”
# OR , you could use the condition- region %in% “region name”

#*****your code here******#

############ ASSIGNMENT # 8 #############
#*Q* Now change the code to subset the data to include states inboth the Midwest and West regions.
# Also, change the ggsave line of code to store it inhousing_line_WestMidwest.pdf file.

#*****your code here******#

############ ASSIGNMENT # 9 #############
#*Q* Now refer back to Assignment #2 for the use of facet_wrap()function.
# Also check out the help for this function if needed.
# Write code below to print a grid of plots for housing value withone plot for each state.
# Make the grid of plots so that there are 10 columns in thegrid.
# Also, change the ggsave line of code to store it inhousing_facet_byState.pdf file.

#*****your code here******#

############ ASSIGNMENT # 10 #############
#*Q* Now change your code so that the plots are faceted by regioninstead of by state.
# Write code below to print a grid of plots for housing value withone plot for each region.
# Make the grid of plots. Choose the number of columns thatgenerates a more lucid grid.
# Also, change the ggsave line of code to store it inhousing_facet_byRegion.pdf file.

#*****your code here******#

############ ASSIGNMENT # 11 #############
#*Q* Now refer back to your code for Assignment #

change your code so that the plots are faceted by region insteadof by state.
# Write code below to print a grid of plots for housing value withone plot for each region.
# Make the grid of plots. Choose the number of columns thatgenerates a more lucid grid.
# Also, change the ggsave line of code to store it inhousing_facet_byRegion.pdf file.

#*****your code here******#

ggplot(housing) +
geom_line(aes(x=Date,
y=Home.Value,
color=State)) +
facet_wrap(~ region, ncol = 3)

# Now we learn to plot multiple values on the same plot. It is likeadding layers
# to your plot.
# In this case, we now also want to plot both the house value andthe land value.
# We limit the plot to only two states to avoid the clutter ofhaving many lines on our plot.
# Two states we have picked – MI and CA.

ggplot(subset(housing, State %in% c(“MI”, “CA”))) +
geom_line(aes(x=Date,
y=Home.Value,
color=State)) +
geom_line(aes(x=Date, y=Land.Value,color=State))

############ ASSIGNMENT # 12 (Linetypes) #############
#*Q* The problem with the plot generated from the above code isthat the plot does not
# clearly tell us which line represents house value and which linerepresents land value.
# Change the above code to bring out the difference between homevalue and land value.
# Hint: Check out the help for the ‘linetype’ parameter ofgeom_line().

#*****your code here******#

############ ASSIGNMENT # 13 (Theme – minimalist)#############

# In this next section we will see how ggplot allows us to applydifferent themes which
# can completely transform the look and feel of a plot.
# check out help for theme on this URL –
#http://www.sthda.com/english/wiki/ggplot2-themes-and-background-colors-the-3-elements

# Now, write code to make a line plot of housing data by date.Facet the plots by state
# in a grid of 10 columns.
# Apply the minimalist theme to your plot – theme_minimal .

############ ASSIGNMENT # 14 (Theme – Black and White)#############
# Apply the black and white theme to your plot – theme_bw .

#*****your code here******#

############ ASSIGNMENT # 15 (Theme – gray) #############
# Apply the gray theme to your plot – theme_gray .

#*****your code here******#

############ ASSIGNMENT # 16 (Theme – dark) #############
# Apply the dark theme to your plot – theme_dark .

#*****your code here******#

############ ASSIGNMENT # 17 (Theme – classic)#############
# Apply the classic theme to your plot – theme_classic .

#*****your code here******#

Expert Answer


Answer to #install.packages(“ggpplot2”) library(ggplot2) library(datasets) ## How to use this project: ## # In this project, some … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *