0% found this document useful (0 votes)
39 views10 pages

Script Syntax

This document discusses various methods for presenting data in R including frequency distribution tables, bar graphs, pie charts, dot plots, stem-and-leaf plots, histograms, cross tabulations, and scatter plots. Examples are provided for each method using sample data imported from CSV files or generated within R code.

Uploaded by

Trisha Mae Pagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

Script Syntax

This document discusses various methods for presenting data in R including frequency distribution tables, bar graphs, pie charts, dot plots, stem-and-leaf plots, histograms, cross tabulations, and scatter plots. Examples are provided for each method using sample data imported from CSV files or generated within R code.

Uploaded by

Trisha Mae Pagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Module 5: Data Presentation

Scripts for RStudio

#install packages

install.packages("readr")

install.packages("pander")

#load packages

library(readr)

library(pander)

# Construction of Frequency Distribution Table (FDT)

#Example 1: Beverages

#Import the files

purchase<-read.csv("purchase.csv")

purchase

#Check the data

View(purchase)

#Frequency Table

#Count the frequency

purchase.freq <-table(purchase)

purchase.freq

#Output: from row to column

freq.dist<-cbind(purchase.freq)

freq.dist
#label the column "Frequency"

colnames(freq.dist)<-c("Frequency")

freq.dist

#Generate the FDT

pander(freq.dist)

#Example 2: Cars

#Creating Vectors, saving to Cars

Cars <-c(1, 2, 1, 0, 3, 4, 0, 1, 1, 2, 2, 3, 2, 1, 4, 0, 0)

Cars

#Determine the Frequency

car.freq <-table(Cars)

car.freq

#Find the relative frequency

car.relfreq<-(car.freq/sum(car.freq))

car.relfreq

#Percent relative frequency

car.percent <-(car.relfreq * 100)

car.percent

#Combine the 3 frequencies

car.fredist <- cbind(car.freq, car.relfreq, car.percent)

car.fredist
#Label the columns

colnames(car.fredist) <-c("Frequency", "Relative Frequency", "Percent Frequency (%)")

car.fredist

#Output

pander(car.fredist)

#Example 3: Grouped Distribution

#Import files

Rent <-read.csv("Rent.csv")

Rent

#Check the stored information

View (Rent)

#Create the Grouped Frequency Distribution Table. (Adjust to get an equal width)

breaks <-seq(425, 621, by = 28)

breaks

#Create the class interval

classint <-cut(Rent$Monthly.Rent, breaks, right=FALSE)

classint

#Determine the number of observations

freq <-table(classint)

freq

#Transform Frequency Table


freq.dist<-cbind(freq)

freq.dist

#Label Columns

colnames(freq.dist)<-c("Frequency")

freq.dist

#Output

pander(freq.dist)

#Bar Graph

#Install packages

install.packages("tidyverse")

install.packages("forcats")

install.packages("readr")

#Load packages

library(readr)

library(tidyverse)

library(forcats)

#Import the file

purchase<-read.csv("purchase.csv")

#Check the stored information

purchase

#Generate the bar chart

bar1<-ggplot(purchase, aes(x=Purchase))+geom_bar(width=0.5)+ggtitle("Soft Drinks")


#Produce the plot

bar1

#Arrange in decreasing order

bar2<- ggplot(mutate(purchase,Purchase=fct_infreq(Purchase)))+geom_bar(aes(x=Purchase))

#Produce the Plot

bar2

#Pie Chart

#Load the package

library(readr)

#Import the file

purchase <-read.csv("purchase.csv")

purchase

#Store the file and get the frequency

data.freq <-table(purchase)

data.freq

#store the frequency

freq <-c(19, 8, 5, 13, 5)

freq

#Label

label <-c("Coke Classic", "Diet Coke", "Dr. Pepper", "Pepsi", "Sprite")

label

#Computing the percentage


percent <-round(freq/sum(freq)*100,1)

percent

#Add the computed percentage to label

label <- paste(label, percent)

label

#Add the percent sign

label <-paste(label, "%", sep ="")

label

#Constructing the Pie Chart

piechart <-pie(freq, labels=label, col=rainbow(length(label)),main="Pie Chart of Soft Drinks Purchases")

#Dot Plot

#package for Dot plot

install.packages("ggplot2")

#Load the package

library(readr)

library(ggplot2)

#If you manually enter the data, use this script

cars<-c(1, 2, 1, 0, 3, 4, 0, 1, 1, 1, 2, 2, 3, 2, 3, 1, 4, 0, 0)

#Check if you really stored the information

cars

#Generating the output when data are manually entered in Rstudio


stripchart(cars, method = "stack", at=c(0.05), pch = 20, cex = 3.2, frame.plot = FALSE, xlim =c(0, 5),
main="Number of Cars Registered")

#Use this script when the file you import file

cars<-read.csv("Cars.csv")

#Check the stored information

cars

#Use this syntax to generate the dot plot when the file is imported

dplot<- ggplot(cars, aes(Cars))+geom_dotplot(binwidth=0.25)

#Check the output

dplot

#Stem and Leaf Plot 1

#Input the data manually in the object named data

data <-c(8.6, 11.7, 9.4, 10.2, 11.0, 8.8)

#Check the stored information

data

#Generate the stem and leaf plot

stem(data)

#Stem and Leaf Plot 2

#Load the package

library(readr)
#Import the file and save to the object named data

data <-read.csv("inflation.csv")

#Check the data

data

#Inspect the data frame

head(data)

#Select only the variable "Inflation" and store in the object named inf

inf <-data$Inflation

#Check the information stored in the object inf

inf

#Generate the stem and leaf plot

output<-stem(inf,scale=2)

#Histogram

#Load the package

library(readr)

#Import file

data <-read.csv("Rent.csv")

#Check the stored information in the object named "data"

data
#Generate the Histogram

hist(data$Monthly.Rent, breaks=seq(425, 625, by=25), main="Histogram of Rents", xlab="Monthly


Rent", ylab="Frequency", col="violet", right=FALSE, ylim=c(0, 30))

#Crosstabulation

#Install the Package

install.packages("summarytools")

#Load the packages

library(readr)

library(summarytools)

library(pander)

#Import files and stored in the object named "Salary"

salary<-read.csv("salaries.csv")

#Check the stored information

salary

#Inspect the data frame

head(salary)

#Syntax to create a cross tabulation of rank and sex

crosstab <-ctable(x=salary$Rank, y=salary$sex, prop="r")

#Check the Result

crosstab
#Generate the cross-tabulation table

pander(crosstab)

#Scatter Diagram/Plot

#Load the package

library(readr)

#Import file and save to the object name "data"

data<-read.csv("Advertising.csv")

#Check the stored information

data

#Inspect the data frame

head(data)

#Syntax to generate the scatter plot

plot(x=data$Number.of.Commercials, y=data$Sale,

xlab="Number of Commercials", ylab="Sales",

main="Number of Commercials vs Sales", xlim=c(0,10), ylim=c(0, 100))

#Add a trend line to the scatter plot

abline(lm(data$Sales~data$Number.of.Commercials))

You might also like