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:
getwd()

#Set work directory:
setwd(“/Users/user/Projects/R-data”) Step 2. Read some data and check the data

#Read CSV data into a variable data1:

#Output the data1:
data1

# Display the structure of data1
str(data1)

Step 3. Draw some plots

# Plot the GPA variable of data1
plot(data1\$GPA)

# Plot the GPA variable against the MathSAT variable
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 GPA hist(data1\$GPA)

# 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) # Create a pie chart of the table of counts of the GPA
pie(table(data1\$GPA))

Are you ready to learn some R now? 🙂