#### Overview of this book

Applied Data Visualization with R and ggplot2 introduces you to the world of data visualization by taking you through the basic features of ggplot2. To start with, you’ll learn how to set up the R environment, followed by getting insights into the grammar of graphics and geometric objects before you explore the plotting techniques. You’ll discover what layers, scales, coordinates, and themes are, and study how you can use them to transform your data into aesthetical graphs. Once you’ve grasped the basics, you’ll move on to studying simple plots such as histograms and advanced plots such as superimposing and density plots. You’ll also get to grips with plotting trends, correlations, and statistical summaries. By the end of this book, you’ll have created data visualizations that will impress your clients.
Title Page
Packt Upsell
Preface
Free Chapter
Basic Plotting in ggplot2
Grammar of Graphics and Visual Components
Advanced Geoms and Statistics
Other Books You May Enjoy
Index

## Chapter 1:  Basic Plotting in ggplot2

The following are the activity solutions for this chapter.

### Activity: Creating a Histogram and Explaining its Features

Steps for Completion:

1. 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.

1. Load the dataset `temperature.csv` from the directory data.
2. Create the histogram for two cities (Vancouver and Miami) by using the command discussed previously.
3. Once the histogram is ready, run the code.
4. 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()
```

### Activity: Creating One- and Two-Dimensional Visualizations with a Given Dataset

Steps for Completion:

1. Load the given datasets and investigate them by using the appropriate commands in dataset: `xAPI-Edu-Data.csv`.
2. Decide which visualizations to use for the given variables: Topic, gender, and VisitedResources.
3. Create one-dimensional visualizations and explain why you chose that type of visual (one per variable). Provide one point of observation for each visualization.
4. 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()```

### Activity: Improving the Default Visualization

Steps for Completion:

1. Use the basic ggplot commands to create two of the plots from Activity B(Topic and VisitedResources).
2. Use the Grammar of Graphics to improve your graphics by layering upon the base graphic. The graph should follow these guidelines:
1. Histograms should be rebinned.
2. Change the fill colors of one- and two-dimensional objects. The line colors should be black.
3. Add a title to the graph.
4. 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)) ```