ggplot2The objective of this assignment is to complete and explain basic plots before moving on to more complicated ways to graph data.
Each question is worth 5 points.
To submit this homework you will create the document in Rstudio,
using the knitr package (button included in Rstudio) and then submit the
document to your Rpubs account. Once
uploaded you will submit the link to that document on Canvas. Please
make sure that this link is hyper linked and that I can see the
visualization and the code required to create it
(echo=TRUE).
mosaicData package.data<-Marriage
str(data)
## 'data.frame': 98 obs. of 15 variables:
## $ bookpageID : Factor w/ 49 levels "B230p1209","B230p1354",..: 5 6 7 8 9 1 2 3 4 16 ...
## $ appdate : Date, format: "1996-10-29" "1996-11-12" ...
## $ ceremonydate : Date, format: "1996-11-09" "1996-11-12" ...
## $ delay : int 11 0 8 5 5 0 16 0 28 10 ...
## $ officialTitle: Factor w/ 9 levels "BISHOP","CATHOLIC PRIEST",..: 4 6 6 7 7 6 6 6 6 7 ...
## $ person : Factor w/ 2 levels "Bride","Groom": 2 2 2 2 2 2 2 2 2 2 ...
## $ dob : Date, format: "1964-04-11" "1964-08-06" ...
## $ age : num 32.6 32.3 34.8 40.6 30 ...
## $ race : Factor w/ 4 levels "American Indian",..: 4 4 3 2 4 4 4 4 2 4 ...
## $ prevcount : int 0 1 1 1 0 1 1 1 0 3 ...
## $ prevconc : Factor w/ 2 levels "Death","Divorce": NA 2 2 2 NA NA 2 2 NA 2 ...
## $ hs : int 12 12 12 12 12 12 12 12 12 12 ...
## $ college : int 7 0 3 4 0 0 0 0 0 6 ...
## $ dayOfBirth : num 102 219 51 141 348 52 284 31 338 183 ...
## $ sign : Factor w/ 12 levels "Aquarius","Aries",..: 2 6 8 5 9 8 7 1 9 3 ...
summary(data$race)
## American Indian Black Hispanic White
## 1 22 1 74
summary(data$sign)
## Aquarius Aries Cancer Capricorn Gemini Leo
## 7 10 8 2 9 7
## Libra Pisces Saggitarius Scorpio Taurus Virgo
## 7 16 9 7 6 10
#Plotting age vs race where x axis is age and y axis iscount of people in that age group. Visual cues are used to distinguish people of different race.
ggplot(data, aes(x = age, fill = race)) +
geom_histogram(binwidth = 1, position = "dodge")
#For Atleast 5 variables:
ggplot(data = data, aes(x = age, y = college)) +
geom_point(aes(color = sign, shape = race), size = 2) +
facet_wrap(~person)
## Warning: Removed 10 rows containing missing values (geom_point).
Your objective for the next four questions will be write the code necessary to exactly recreate the provided graphics.
This boxplot was built using the mpg dataset. Notice the
changes in axis labels.
mpgdata<-mpg
mpgdata
## # A tibble: 234 × 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto… f 18 29 p comp…
## 2 audi a4 1.8 1999 4 manu… f 21 29 p comp…
## 3 audi a4 2 2008 4 manu… f 20 31 p comp…
## 4 audi a4 2 2008 4 auto… f 21 30 p comp…
## 5 audi a4 2.8 1999 6 auto… f 16 26 p comp…
## 6 audi a4 2.8 1999 6 manu… f 18 26 p comp…
## 7 audi a4 3.1 2008 6 auto… f 18 27 p comp…
## 8 audi a4 quattro 1.8 1999 4 manu… 4 18 26 p comp…
## 9 audi a4 quattro 1.8 1999 4 auto… 4 16 25 p comp…
## 10 audi a4 quattro 2 2008 4 manu… 4 20 28 p comp…
## # … with 224 more rows
library(ggplot2)
plot <- ggplot(mpgdata,aes(manufacturer,hwy))
plot + geom_boxplot()+coord_flip()+labs(x="Vehicle Manufacturer", y="Highway Fuel Efficiency(miles/gallon) ")+theme_classic()
This graphic is built with the diamonds dataset in the
ggplot2 package.
library(ggthemes)
library(ggplot2)
diamonds_data<-diamonds
diamonds_data
## # A tibble: 53,940 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
## 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39
## # … with 53,930 more rows
plot3 <- ggplot(diamonds, aes(price, colour = cut, fill = cut)) +
geom_density(alpha = 0.2) +
scale_fill_discrete() +
labs(x = "Diamond Price ", y = "Density", title = "Diamond Price Density")+
theme_bw()
plot3
This graphic uses the penguins dataset and shows the
counts between males and females by species.
penguins<-penguins
penguins
## # A tibble: 344 × 8
## species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
## <fct> <fct> <dbl> <dbl> <int> <int>
## 1 Adelie Torgersen 39.1 18.7 181 3750
## 2 Adelie Torgersen 39.5 17.4 186 3800
## 3 Adelie Torgersen 40.3 18 195 3250
## 4 Adelie Torgersen NA NA NA NA
## 5 Adelie Torgersen 36.7 19.3 193 3450
## 6 Adelie Torgersen 39.3 20.6 190 3650
## 7 Adelie Torgersen 38.9 17.8 181 3625
## 8 Adelie Torgersen 39.2 19.6 195 4675
## 9 Adelie Torgersen 34.1 18.1 193 3475
## 10 Adelie Torgersen 42 20.2 190 4250
## # … with 334 more rows, and 2 more variables: sex <fct>, year <int>
plot4<-penguins %>%
count(sex, species) %>%
ggplot() +
geom_col(aes(x = sex, y = n, fill = species)) +
scale_fill_manual(values = c("darkorange", "purple", "cyan4")) +
facet_wrap(~species, ncol = 1) +
theme_minimal() +
theme(legend.position = 'none') +
labs(x = "Sex", y = "Count") +
coord_flip()
plot4
This figure examines the relationship between bill length and depth
in the penguins dataset.
plot5<-ggplot(data = penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_point(aes(shape = species), size = 2) +
geom_smooth(method = 'lm', se = FALSE) +
scale_color_manual(values = c("darkorange", "darkorchid", "cyan4")) +
labs(x = "Bill Length (mm)", y = "Bill Depth (mm)", color = "Species", shape = "Species")
plot5