Chapter 4 Methods

library(tidyverse)
library(ggplot2)

Problem 1

ggplot(data= mpg, aes(x = hwy, fill=drv))+
  geom_histogram(alpha=0.5)+
  labs(title = "Histogram",
       subtitle = "Histogram of Highway Mile Per Gallon",
       caption = "Source: mpg")+
  theme_minimal()
## `stat_bin()` using `bins = 30`. Pick better value
## with `binwidth`.

Problem 2

ggplot(data= mpg, aes(x = hwy, fill=drv))+
  geom_histogram(alpha=0.5)+
  facet_grid(rows=vars(drv))+
  theme_minimal()+
  labs(title = "Histogram using facet_grid()",
       subtitle = "Histogram of Highway Mile Per Gallon",
       caption = "Source: mpg")
## `stat_bin()` using `bins = 30`. Pick better value
## with `binwidth`.

Problem 3

options(scipen = 999)
ggplot(data= midwest, aes(x = area, y = poptotal)) +
  geom_point(alpha = 0.4,aes(color = state, size = popdensity))+
  scale_x_continuous(limits = c(0,0.1))+
  scale_y_continuous(limits = c(0, 500000))+
  theme_classic()+
  labs(title = "Scatterplot",
       subtitle = "Area Vs Population",
       caption = "Source: midwest",
       y = "Population", 
       x = "Area")+
  geom_smooth(aes(group=1),se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 15 rows containing non-finite values
## (stat_smooth).
## Warning: Removed 15 rows containing missing values
## (geom_point).

Problem 4

ggplot(data= iris,aes(x = Sepal.Length, y = Sepal.Width, color=Species, shape= Species))+
  geom_point(alpha = 0.5, size=6)+
  labs(title = "Scatterplot",
       subtitle = "Sepal.Length Vs Sepal.Width",
       caption = "Source:iris",
       y = "Sepal.Width", 
       x = "Sepal.Length")+
  theme_minimal()

Problem 5

library(gcookbook)
head(heightweight)
##   sex ageYear ageMonth heightIn weightLb
## 1   f   11.92      143     56.3     85.0
## 2   f   12.92      155     62.3    105.0
## 3   f   12.75      153     63.3    108.0
## 4   f   13.42      161     59.0     92.0
## 5   f   15.92      191     62.5    112.5
## 6   f   14.25      171     62.5    112.0
ggplot(data=heightweight,aes(x=heightIn, y=weightLb, color = sex))+
  geom_point(alpha=0.5, size=3)+geom_smooth(method = "lm", se=FALSE)+
  labs(title = "Scatterplot",
       subtitle = "Weight Vs Height",
       caption = "source: heightweight",
       y = "weightLb", 
       x = "heightIn")+
  theme_classic()
## `geom_smooth()` using formula 'y ~ x'

Problem 6

library(RColorBrewer)
ggplot(data=mpg, aes(x=manufacturer, fill= class,color = class))+
  geom_bar(width = 0.5)+
  labs(title = "Barplot",
       subtitle = "Manufacturer across Vehicle Classes",
       y = "count", 
       x = "manufacturer")+
  scale_color_brewer(palette = "Spectral")+
  theme_minimal()+
  theme(axis.text.x = element_text(angle=65, hjust=1))+
  scale_fill_brewer(palette = "Spectral")