The following are the activity solutions for this chapter.
Steps for Completion:
- Use the
RestaurantTips
dataset inLock5data
. - Compare the TIP amount for various days. Use
aes=color
forgeom_density
command. - Superimpose all of the plots.
- Use the
scale_x_continuous
command for the x-axis tick marks.
Steps for Completion:
- Use the
strftime
command to get the month from each date and make another variable (Month
), as follows:
df_fb$Month <- strftime(df_fb$Date,"%m")
- Change the month to a numerical value by using
as.numeric
:
df_fb$Month <- as.numeric(df_fb$Month)
- Now, use ggplot to make a plot of closing prices versus months.
- Plot the data using
geom_point (color=red)
. - Change the x scale to show each month, and label the x-axis, such that each month is shown.
- Title your plot Monthly closing stock prices: Facebook.
- Use
geom_line(stat='summary',fun.y=mean)
to plot the mean.
Outcome:
The complete code is shown as follows:
ggplot(df_fb, aes(Month,Close)) + geom_point(color="red",alpha=1/2,position = position_jitter(h=0.0,w=0.0 ))+ geom_line(stat='summary',fun.y=mean, color="blue",size=1)+ scale_x_continuous(breaks=seq(0,13,1))+ ggtitle("Monthly Closing Stock Prices: Facebook")+theme_classic()
Steps for Completion:
- Merge the
USStates
data withstates_map
. - Before merging, change the
states
variable inUSStates
to the same format used instates_map
.
- Use the ggplot options
geom_polygon
andcoord_map
to create the map. - For aesthetics, run the following code and specify
x=long
,y=lat
,group=group
, andfill=ObamaVote
.
Outcome:
The complete code is shown as follows:
USStates$Statelower <- as.character(tolower(USStates$State)) glimpse(USStates) us_data <- merge(USStates,states_map,by.x="Statelower",by.y="region") head(us_data)
Steps for Completion:
- Make a subset of the
loan
dataset by using some of the following variables:
df3_1 <- df3[,c("funded_amnt","annual_inc","dti","inq_last_6mths", "total_acc","total_pymnt_inv")]
- Use
cor
for the precedingloan
data subset, and then choose two highly correlated variables in theloan
dataset. Use pairs, as follows:
total_rec_prncp and total_pymnt_int funded_amnt,total_pymnt_inv
- Make a scatterplot for the preceding pairs for grade A, then fit a linear regression model.
- Determine what are the correlations of the preceding pairs.
Outcome:
Answer to step 4: The correlations are as follows:
- 93%
- 85%