2017-11-13 20_49_03-Plot Zoom.png

The end result 

Copy and paste the following code to your R Studio platform or R version:

# Making a plot in R using Base R
head(mtcars)
plot(mtcars$wt, mtcars$mpg)
abline(lm(mpg ~ wt, data = mtcars))

plot(mtcars$wt, mtcars$mpg, main = “Miles Per Gallon and Weight”,
xlab = “Weight(1000lbs)”, ylab = “Miles per gallon”,
pch = “*”, col = “blue”, col.main = “royalblue2”,
col.lab = “brown”, col.axis = “black”)

# You cans see all the available colours by typing the following command
colors()

# Increasing the size of the plot happens with the cex argument inserted
plot(mtcars$wt, mtcars$mpg, main = “Miles Per Gallon and Weight”,
xlab = “Weight(1000lbs)”, ylab = “Miles per gallon”,
pch = “o”, col = “blue”, col.main = “royalblue2”,
col.lab = “brown”, col.axis = “black”,
cex = 2, cex.axis = 1.5, cex.lab = 1.5)

# If you want to add text, you add it once you plot the relationship
plot(mtcars$wt, mtcars$mpg, main = “Miles Per Gallon and Weight”,
xlab = “Weight(1000lbs)”, ylab = “Miles per gallon”,
pch = “o”, col = “blue”, col.main = “royalblue2”,
col.lab = “brown”, col.axis = “black”,
cex = 2, cex.axis = 1.5, cex.lab = 1.5)

text(mtcars$wt, mtcars$mpg + 1,
labels = rownames(mtcars), cex = 0.7)
text(4.5, 30, labels = “Car Makes”, cex = 4, col = “red”)

# mtext refers to the text in the margin of the plot-side 1 is the x axis
plot(mtcars$wt, mtcars$mpg, main = “Miles Per Gallon and Weight”,
xlab = “Weight(1000lbs)”, ylab = “Miles per gallon”,
pch = “o”, col = “blue”, col.main = “royalblue2”,
col.lab = “brown”, col.axis = “black”,
cex = 2, cex.axis = 1.5, cex.lab = 1.5)

text(mtcars$wt, mtcars$mpg + 1,
labels = rownames(mtcars), cex = 0.7)
text(4.5, 30, labels = “Car Makes”, cex = 4, col = “red”)
mtext(“Assume that you have another axis here (dual)”,
side = 4, cex = 1.5, col = “magenta1”)

# Dual Axis plots
set.seed(1001)
year <- 1901:1920
population <- c(1:20)
gdp <- sample(40:50, 20, replace = TRUE)

year
population
gdp

plot.new()
par(mar = c(5, 4, 4, 5) + 0.1)
plot(year, population, type = “b”, ylim = c(0, 21),
lwd = 2, main = “Multiple Y Axes”, col = “green3”,
pch = 16)
# By assigning this par(new = TRUE) we do not overwrite the previous plot
# Merely, we continue adding to it
par(new = TRUE)
plot(year, gdp, type = “b”, axes = FALSE, xlab = “”, ylab = “”,
ylim = c(30, 60), lwd = 2, main = “Multiple Y Axes”, col = “red”,
pch = 15)
axis(side = 4, ylim = c(30, 60)) # draw the second axis like this
mtext(“gdp”, side = 4, line = 3)
legend(“bottomright”, inset = 0.05, col = c(“green3”, “red”),
lty = 1, legend = c(“Pop”, “GDP”))

Posted by Dimitris

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

w

Connecting to %s