library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.2
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.2
## -- Attaching packages -------------------------------- tidyverse 1.2.1 --
## v tibble 1.4.2 v purrr 0.2.5
## v tidyr 0.8.2 v dplyr 0.7.8
## v readr 1.3.0 v stringr 1.3.1
## v tibble 1.4.2 v forcats 0.3.0
## Warning: package 'tidyr' was built under R version 3.5.2
## Warning: package 'purrr' was built under R version 3.5.2
## Warning: package 'dplyr' was built under R version 3.5.2
## Warning: package 'stringr' was built under R version 3.5.2
## Warning: package 'forcats' was built under R version 3.5.2
## -- Conflicts ----------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
str(mpg)
## Classes 'tbl_df', 'tbl' and 'data.frame': 234 obs. of 11 variables:
## $ manufacturer: chr "audi" "audi" "audi" "audi" ...
## $ model : chr "a4" "a4" "a4" "a4" ...
## $ displ : num 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
## $ year : int 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
## $ cyl : int 4 4 4 4 6 6 6 4 4 4 ...
## $ trans : chr "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
## $ drv : chr "f" "f" "f" "f" ...
## $ cty : int 18 21 20 21 16 18 18 18 16 20 ...
## $ hwy : int 29 29 31 30 26 26 27 26 25 28 ...
## $ fl : chr "p" "p" "p" "p" ...
## $ class : chr "compact" "compact" "compact" "compact" ...
#Making a table of counts of each type
table(mpg$manufacturer)
##
## audi chevrolet dodge ford honda hyundai
## 18 19 37 25 9 14
## jeep land rover lincoln mercury nissan pontiac
## 8 4 3 4 13 5
## subaru toyota volkswagen
## 14 34 27
#You might like to know the proportion of each type of car, and dividing by 234 isn't such a simple thing to do in your head (at least, not for everyone!). Luckily, you can pass the table to the R function prop.table to convert all these numbers into proportions:
prop.table(table(mpg$manufacturer))
##
## audi chevrolet dodge ford honda hyundai
## 0.07692308 0.08119658 0.15811966 0.10683761 0.03846154 0.05982906
## jeep land rover lincoln mercury nissan pontiac
## 0.03418803 0.01709402 0.01282051 0.01709402 0.05555556 0.02136752
## subaru toyota volkswagen
## 0.05982906 0.14529915 0.11538462
ggplot(mpg,aes(manufacturer)) +
geom_bar() +
theme(text = element_text(size = 30), axis.text.x = element_text(angle = 90))

#I will teach you more about ggplot. For the moment, this command is telling R to:
#1. Make a canvas which will visualise data from the mpg dataframe ggplot(mpg)
#2. Map the single aesthetic manufacturer to the x-axis of this canvas aes(manufacturer), and
#3. Add a barchart layer showing this to the plot + geom_bar().
Demo Activity mpg data.frame
## Get libraries------------------------------------------------------
library(tidyverse)
## Get mpg data
data(mpg)
##Tables--------------------------------------------------------------
table(mpg$manufacturer)
##
## audi chevrolet dodge ford honda hyundai
## 18 19 37 25 9 14
## jeep land rover lincoln mercury nissan pontiac
## 8 4 3 4 13 5
## subaru toyota volkswagen
## 14 34 27
prop.table(table(mpg$manufacturer))
##
## audi chevrolet dodge ford honda hyundai
## 0.07692308 0.08119658 0.15811966 0.10683761 0.03846154 0.05982906
## jeep land rover lincoln mercury nissan pontiac
## 0.03418803 0.01709402 0.01282051 0.01709402 0.05555556 0.02136752
## subaru toyota volkswagen
## 0.05982906 0.14529915 0.11538462
##Bar Charts----------------------------------------------------------
ggplot(mpg,aes(manufacturer))+geom_bar()

Activity STARWARS data.frame: Make tables using a preloaded dataset
#Instructions
#This activity has 2 parts.
#Part 1 - Follow these steps to generate your outputs for this activity:
#Load the starwars data.frame contained in the tidyverse package, and
#Use the starwars data.frame to produce two tables; the first table should show the number of characters with each eye colour, and the second table should show the proportion of characters with each eye colour.
#Part 2 - Once you have generated your outputs, answer the questions.
## Get libraries------------------------------------------------------
library(tidyverse)
## Get starwars data
data(starwars)
##Tables--------------------------------------------------------------
table(starwars$eye_color)
##
## black blue blue-gray brown dark
## 10 19 1 21 1
## gold green, yellow hazel orange pink
## 1 1 3 8 1
## red red, blue unknown white yellow
## 5 1 3 1 11
prop.table(table(starwars$eye_color))
##
## black blue blue-gray brown dark
## 0.11494253 0.21839080 0.01149425 0.24137931 0.01149425
## gold green, yellow hazel orange pink
## 0.01149425 0.01149425 0.03448276 0.09195402 0.01149425
## red red, blue unknown white yellow
## 0.05747126 0.01149425 0.03448276 0.01149425 0.12643678
#Q:1 How many characters in the starwars data.frame have orange eyes?
#Ans : 8
#Q:2 What is the most common eye colour amongst characters in the starwars data.frame?
#Ans : Brown
#Q:3 What proportion of characters in the starwars data.frame have blue eyes?
#Ans : 0.218
#Q:4 What proportion of characters in the starwars data.frame have unknown eyes?
#Ans : 0.034
#Q:5 What percentage of characters in the starwars data.frame have black eyes?
#Ans :11.494