The following are the activity solutions for this chapter.
Steps for Completion:
- Use the template code
Lesson1_student.R
.
Note
This is an empty code, wherein the libraries are already loaded. You will be writing your code here.
- Load the dataset
temperature.csv
from the directory data. - Create the histogram for two cities (Vancouver and Miami) by using the command discussed previously.
- Once the histogram is ready, run the code.
- Analyze the two histograms by giving three points for each histogram, and two points of difference between the two.
Outcome:
Two histograms should be created and compared. The complete code is as follows:
df_t <- read.csv("data/historical-hourly-weather-data/temperature. csv") ggplot(df_t,aes(x=Vancouver))+geom_histogram() ggplot(df_t,aes(x=Miami))+geom_histogram()
Steps for Completion:
- Load the given datasets and investigate them by using the appropriate commands in dataset:
xAPI-Edu-Data.csv
. - Decide which visualizations to use for the given variables: Topic, gender, and VisitedResources.
- Create one-dimensional visualizations and explain why you chose that type of visual (one per variable). Provide one point of observation for each visualization.
- Create two-dimensional boxplots or scatterplots for VisitedResources versus Topic, VisitedResources versus AnnouncementsView, and Discussion versus Gender. What are your observations? Write at least five points.
Outcome:
Three one-dimensional plots and three two-dimensional plots should be created, with the following axes (count versus topic) and observations. (Note that the students may provide different observations, so the instructor should verify the answers.)
The complete code is as follows:
df_edu <- read.csv("data/xAPI-Edu-Data.csv") str(df_edu) #Functions for Plotting a barchart/Histogram plotbar <- function(df,mytxt) { ggplot(df,aes_string(x=mytxt)) + geom_bar() } plothist <- function(df,mytxt) { ggplot(df,aes_string(x=mytxt)) + geom_histogram() } #Alternatively one can use a function to plot but students can just #do it directly at this point. #1-D Plots plotbar(df_edu,"Topic") plotbar(df_edu,"gender") plotbar(df_edu,"ParentschoolSatisfaction") plothist(df_edu,"VisitedResources") #2-D Plots ggplot(df_edu,aes(x=Topic,y=VisitedResources)) + geom_boxplot() ggplot(df_edu,aes(x=AnnouncementsView,y=VisitedResources)) + geom_point() ggplot(df_edu,aes(x=gender,y=Discussion)) + geom_boxplot()
Steps for Completion:
- Use the basic ggplot commands to create two of the plots from Activity B(Topic and VisitedResources).
- Use the Grammar of Graphics to improve your graphics by layering upon the base graphic. The graph should follow these guidelines:
- Histograms should be rebinned.
- Change the fill colors of one- and two-dimensional objects. The line colors should be black.
- Add a title to the graph.
- Apply the appropriate font sizes and colors to the x- and y-axes.
Outcome:
The complete code is as follows:
p1 <- ggplot(df_edu,aes(x=Topic)) p2 <- ggplot(df_edu,aes(x=VisitedResources)) p1 + geom_bar(color=1,fill=3) + ylab("Count")+ theme(axis.text.y=element_text(size=10), axis.text.x=element_text(size = 10), axis.title.x=element_text(size=15,color=4), axis.title.y=element_text(size=15,color=4))+ ggtitle("Topics in Education data") p2 + geom_histogram(bins=20,fill="white",color=1)+ ggtitle("Visited Resources for Education data")+ xlab("Visited Resources")+ theme(axis.text.x=element_text(size = 12), axis.text.y=element_text(size=12), axis.title.x=element_text(size=15,color=4), axis.title.y=element_text(size=15,color=4))