Learn R – Part 3

Plots visualize trends and relationships. Use plot() for basic graphs, lines()/points() for overlays, and par() for layouts. Customize with main, xlab, ylab, col, lwd, pch, and bg.

Demo Data (Basic)
# Simple dataset for beginners  
x <- 1:10  

y <- c(2, 4, 6, 8, 7, 5, 3, 1, 9, 10)  
Demo Data (Advanced)
# Complex dataset: mtcars (built-in)  
data(mtcars)  
mpg <- mtcars$mpg  # Miles per gallon  
hp <- mtcars$hp    # Horsepower  

wt <- mtcars$wt    # Weight  
Practice
# BASIC TASKS  
# HW1: Plot x vs. y as points  
# HW2: Add a blue line to the plot  
# HW3: Create a plot with red triangles (pch=17)  

# ADVANCED TASKS  
# HW4: Plot mpg vs. hp from mtcars, add a smooth line  
# HW5: Create a multi-plot layout (2x2 grid) 

# HW6: Customize mpg vs. wt plot: title, axis labels, green points, gray background  
Solution
R
# BASIC SOLUTIONS  
plot(x, y)  
plot(x, y, type="l", col="blue")  
plot(x, y, pch=17, col="red")  

# ADVANCED SOLUTIONS  
plot(mpg ~ hp, data=mtcars, main="MPG vs Horsepower", col="purple")  
lines(lowess(hp, mpg), col="orange")  

par(mfrow=c(2,2))  
plot(mpg ~ hp, data=mtcars)  
plot(mpg ~ wt, data=mtcars)  
hist(mpg, col="lightblue")  
boxplot(mpg, col="yellow")  

plot(mpg ~ wt, data=mtcars,  
     main="Weight vs MPG",  
     xlab="Weight (1000 lbs)",  
     ylab="Miles Per Gallon",  
     pch=21, bg="green",  
     panel.first=grid())  
HW1: Plotting of (x, y)
HW2: Adding a blue line to the plot
HW3: A plot with red triangles (pch=17)
HW4: Plot mpg vs. hp from mtcars, add a smooth line
HW5: A multi-plot layout (2×2 grid)
HW6: Customization mpg vs. wt plot

Pie charts show proportions. Use pie(), legend(), and ifelse() for conditional formatting.

Demo Data (Basic)
# Simple sales data  
sales <- c(25, 35, 40)  

labels <- c("Apparel", "Electronics", "Groceries")  
Demo Data (Advanced)
# Complex dataset: Titanic survival rates  
survivors <- c(203, 118, 178, 528)  

groups <- c("1st Class", "2nd Class", "3rd Class", "Crew")  
Practice
# BASIC TASKS  
# HW1: Create a pie chart for sales data  
# HW2: Add a title and explode the "Groceries" slice  

# ADVANCED TASKS  
# HW3: Plot Titanic survival rates with gradient colors  
# HW4: Add a legend and percentage labels  

# HW5: Create a 3D pie chart (use plotrix package)  
Solution
R
# BASIC SOLUTIONS  
pie(sales, labels=labels)  
pie(sales, labels=labels, main="Sales Distribution", explode=0.1)  

# ADVANCED SOLUTIONS  
# Install the plotrix package (only needed once)
install.packages("plotrix")

# Load the package
library(plotrix)

library(plotrix)  
pie3D(survivors, labels=groups, main="Titanic Survival Rates",  
      col=heat.colors(4), explode=0.05)  

pie(survivors, labels=paste(groups, " (", round(survivors/sum(survivors)*100, 1), "%)", sep=""),  
    col=rainbow(4))  
legend("right", groups, fill=rainbow(4))  
HW1: A pie chart for sales data
HW2: Adding a title and explode the “Groceries” slice
HW3: Titanic survival rates with gradient colors
HW4: Legend and percentage labels

Bar charts compare categories. Use barplot(), beside=TRUE for grouped bars, and col for gradients.

Demo Data (Basic)
# Monthly sales  
months <- c("Jan", "Feb", "Mar")  

sales <- c(200, 450, 300)  
Demo Data (Advanced)
# Complex dataset: Olympic medal counts  
countries <- c("USA", "China", "Russia", "UK")  
gold <- c(39, 38, 20, 22)  
silver <- c(41, 32, 28, 21)  

bronze <- c(33, 18, 23, 22)  
Practice
# BASIC TASKS  
# HW1: Create a vertical bar chart for monthly sales  
# HW2: Add grid lines and rotate labels  

# ADVANCED TASKS  
# HW3: Create stacked bars for Olympic medals  
# HW4: Create grouped bars with legends  

# HW5: Add error bars using arrows()  
Solution
R
# BASIC SOLUTIONS  
barplot(sales, names.arg=months, main="Monthly Sales", xlab="Month", ylab="Revenue")  
barplot(sales, names.arg=months, las=2, cex.names=0.8, col="lightgreen")  
abline(h=seq(0, 500, by=100), lty=2)  

# ADVANCED SOLUTIONS  
medals <- rbind(gold, silver, bronze)  
barplot(medals, names.arg=countries, col=c("gold", "silver", "darkorange"),  
        legend=rownames(medals), main="Olympic Medals", beside=TRUE)  

# Error bars  
barplot(gold, names.arg=countries, ylim=c(0, 50), col="gold")  
arrows(x0=1:4, y0=gold-2, y1=gold+2, code=3, angle=90, length=0.1)  
HW1: A vertical bar chart for monthly sales
HW2: Adding grid lines and rotate labels
HW3: Stacked bars for Olympic medals
HW4: Add error bars using arrows()

Leave a Reply

Your email address will not be published. Required fields are marked *