Stacked bar chart Plot the relationship between newContruction 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)
## Observations: 1,728
## Variables: 16
## $ price <int> 132500, 181115, 109000, 155000, 86060, 120000, 1…
## $ lotSize <dbl> 0.09, 0.92, 0.19, 0.41, 0.11, 0.68, 0.40, 1.21, …
## $ age <int> 42, 0, 133, 13, 0, 31, 33, 23, 36, 4, 123, 1, 13…
## $ landValue <int> 50000, 22300, 7300, 18700, 15000, 14000, 23300, …
## $ livingArea <int> 906, 1953, 1944, 1944, 840, 1152, 2752, 1662, 16…
## $ pctCollege <int> 35, 51, 51, 51, 51, 22, 51, 35, 51, 44, 51, 51, …
## $ bedrooms <int> 2, 3, 4, 3, 2, 4, 4, 4, 3, 3, 7, 3, 2, 3, 3, 3, …
## $ fireplaces <int> 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ bathrooms <dbl> 1.0, 2.5, 1.0, 1.5, 1.0, 1.0, 1.5, 1.5, 1.5, 1.5…
## $ rooms <int> 5, 6, 8, 5, 3, 8, 8, 9, 8, 6, 12, 6, 4, 5, 8, 4,…
## $ heating <fct> electric, hot water/steam, hot water/steam, hot …
## $ fuel <fct> electric, gas, gas, gas, gas, gas, oil, oil, ele…
## $ sewer <fct> septic, septic, public/commercial, septic, publi…
## $ waterfront <fct> No, No, No, No, No, No, No, No, No, No, No, No, …
## $ newConstruction <fct> No, No, No, No, Yes, No, No, No, No, No, No, No,…
## $ centralAir <fct> No, No, No, No, Yes, No, No, No, No, No, No, No,…
Stacked bar chart Plot the relationship between newContruction and heating type.Hint: See the code in 4.1.1 Stacked bar chart.
library(ggplot2)
# 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 overall is the hot air heating system. The hot air heating system is the only heating system used for new construction, and has the highest count in the old construction. ## Q3 Grouped bar chart Plot the relationship between newContruction and heating type. Hint: See the code in 4.1.2 Grouped bar chart.
library(ggplot2)
# grouped bar plot
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.
library(ggplot2)
# bar plot, with each bar representing 100%
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. The proportion of hot air heating system is larger in new constructions. More of the total number of heating systems for new constructions is hot air as compared to previously built houses. ## Q6 Rename the construction type as new and old. Hint: See the code in 4.1.4 Improving the color and labeling.
library(ggplot2)
# bar plot, with each bar representing 100%
ggplot(SaratogaHouses,
aes(x = factor(newConstruction,
labels = c("New",
"Old")),
fill = heating)) +
geom_bar(position = "fill") +
labs(y = "Proportion")
Hint: See the code in 4.1.4 Improving the color and labeling.
library(ggplot2)
# bar plot, with each bar representing 100%
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 = "Construction Type",
title = "Heating Types of Different Constructions") +
theme_minimal()
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.