Stacked bar chart Plot the relationship between newConstruction and heating type.Segmented bar chart Plot the relationship between newContruction and heating type.In this exercise you will learn to plot data using the ggplot2 package. To this end, you will make your own note of 4.1 Categorical vs. Categorical from Data Visualization with R.
# Load package
library(tidyverse)
# Load data
data(SaratogaHouses, package="mosaicData")
glimpse(SaratogaHouses)
Stacked bar chart Plot the relationship between newConstruction and heating type.Hint: See the code in 4.1.1 Stacked bar chart.
ggplot(SaratogaHouses,
aes(x = newConstruction,
fill = heating)) +
geom_bar(position = "stack")
Hint: See the stacked bar chart you created in the previous question. The most common heating system is hot air ## Q3 Grouped bar chart Plot the relationship between newContruction and heating type. Hint: See the code in 4.1.2 Grouped bar chart.
ggplot(SaratogaHouses,
aes(x = newConstruction,
fill = heating)) +
geom_bar(position = "dodge")
Segmented bar chart Plot the relationship between newContruction and heating type.Hint: See the code in 4.1.3 Segmented bar chart.
ggplot(SaratogaHouses,
aes(x = newConstruction,
fill = heating)) +
geom_bar(position = "fill") +
labs(y = "Proportion")
Hint: See the segmented bar chart you created in the previous question. In the new houses hot air is the higher heating system ## Q6 Rename the construction type as new and old. Hint: See the code in 4.1.4 Improving the color and labeling.
ggplot(SaratogaHouses,
aes(x = factor(newConstruction,
labels = c("new",
"old")),
fill = heating)) +
geom_bar(position = "fill") +
labs(y = "Proportion")
## Q7 Add labels to the axes. Hint: See the code in 4.1.4 Improving the color and labeling.
ggplot(SaratogaHouses,
aes(x = factor(newConstruction,
labels = c("new",
"old")),
fill = heating)) +
geom_bar(position = "fill") +
labs(y = "Proportion") +
labs(y = "Percent",
fill = "Heating Types",
x = "Home Type",
title = "Types of Home Heating")
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.