The Power of R

Here I would like to share Five Steps in Twelve lines of R code to draw some plots, histograms, boxplot and pie chart (so many stuff!).  It is super easy and convenient! I am sure you will be impressed by the R power, just like I do.

Environment: R Studio
Data Source: Rdatasets (A great place to download existing data for practice).

I will be using the SATGPA dataset to see if there is any correlation between students’ GPA and SAT score. Here we go:

Step 1. Set up our work directory:

# Get work directory:

#Set work directory:

Screen Shot 2017-08-03 at 11.03.23 AMStep 2. Read some data and check the data

#Read CSV data into a variable data1:
data1 <- read.csv(“csv/Stat2Data/SATGPA.csv”, header = TRUE)

#Output the data1:

# Display the structure of data1


Step 3. Draw some plots

# Plot the GPA variable of data1

# Plot the GPA variable against the MathSAT Screen Shot 2017-08-03 at 11.05.31 AMvariable
plot(data1$GPA, data1$MathSAT)

# Plot the GPA variable against the VerbalSAT variable
plot(data1$GPA, data1$VerbalSAT)

Step 4. How about some histograms!

# Create a histogram for GPAScreen Shot 2017-08-03 at 11.06.51 AM

# Create a histogram for rating, with 20 bins
hist(data1$GPA, breaks = 20)

Step 5. boxplot and pie Chart!

# Create a boxplot of the GPA variable
boxplot(data1$GPA)Screen Shot 2017-08-03 at 11.07.44 AM

# Create a pie chart of the table of counts of the GPA

Are you ready to learn some R now? 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s