You are on page 1of 2

=mpg, color=drv, shape=drv) # data points color and shape based on the variable

"drv (drive type)"

# Add smoothing functions or trend lines to the scatter plot


qplot(displ, hwy, data=mpg, color=drv, geom=c("point", "smooth")) # "point"
parameter in geom argument refers to the data points and "smooth" to the trend
lines to be plotted
# The gray areas surrounding each trend lines indicate the 95% confidence intervals
for the lines

# Scatter plot without x-axis


qplot(y=hwy, data=mpg, color=drv) # Specifying the y parameter only plots the
values of the y argument in the order in which they occur in the data

# Box and whisker plot


qplot(drv, hwy, data=mpg, geom="boxplot") # drv is the variable by which data is
split, hwy is the variable to be examined
qplot(drv, hwy, data=mpg, geom="boxplot", color=manufacturer) # Each region of the
plot (determined by the factor drv) is subdivided into several boxes depicting
different manufacturers

# Histogram plot
qplot(hwy, data=mpg, fill=drv) # frequency count variable = hwy, 3 different drive
factors are distinguished based on the color or fill factor

# Plots with facets or panels


qplot(displ, hwy, data=mpg, facets=.~drv) # . ~ drv is ggplot2's shorthand for
number of rows (to the left of the ~) and number of columns (to the right of the
~).
# Here the . indicates a single row and drv implies 3,
since there are 3 distinct drive factors i.e. 1 by 3 array of plots
qplot(hwy, data=mpg, facets=drv~., binwidth=2) # 3 histograms of frequency count
"hwy" plotted along 3 rows, a histogram for each drive factor "drv"

###################################### Plotting using ggplot() function


#####################################

# Create a graphical object using ggplot() function and assign it to a variable g


g <- ggplot(data=mpg, aes(displ, hwy)) # aes() functions contain the variables
"displ" and "hwy" we want our aesthetics to represent

# Plot a scatter plot


g + geom_point() # geom_point() function adds a layer of scatter plot to the
graphical object g; No arguments were passed to the function geom_point() since the
object g has all the data stored in it

# Add a smoothing or trending line


g+geom_point()+geom_smooth() # dd a trending line with default arguments
g+geom_point()+geom_smooth(method="lm") # By changing the smoothing function to
"lm" (linear model) ggplot2 generates a regression line through the data

# Plot facets based on the drive factor "drv"


g+geom_point()+geom_smooth(method="lm")+facet_grid(.~drv)

# Add a title to the plot


g+geom_point()+geom_smooth(method="lm")+facet_grid(.~drv)+ggtitle("Swirl Rules!")

# Add aesthetics to geom_point function


g+geom_point(aes(color=drv), size=4, alpha=1/2)
# Add aesthetics, plot title and labels for x and y axes
g + geom_point(aes(color=drv), size=2) + labs(title="Swirl Rules!") +
labs(x="Displacement", y="Hwy Mileage")

# Modify smoothing line using geom_smooth() function


g + geom_point(aes(color=drv), size=2) + geom_smooth(linetype=2, method="lm",
se=TRUE) # se parameter can be set to FALSE to turn off the gray shadows indicating
standard errors (confidence intervals)

# change the theme to black and white using theme_bw() function, theme fonts are
also changed (default theme = theme_gray())
g + geom_point(aes(color = drv)) + theme_bw(base_family="Times")

############## Another plot example for ggplot() function ##############

# Create a graphical object


g <- ggplot(mpg, aes(x=displ, y=hwy, color=factor(year))) # The color parameter
will allow us to distinguish between the two manufacturing years in our data

# Plot a scatter plot


g + geom_point()

# Plot facets
g + geom_point() + facet_grid(drv~cyl, margins=TRUE) # The margins argument tells
ggplot to display the marginal totals over each row and column, so instead of
seeing
# 3 rows (the number of drv factors) and 4
columns (the number of cyl factors) we see a 4 by 5 display.

# Add smoothing lines


g + geom_point() + facet_grid(drv~cyl, margins=TRUE) + geom_smooth(method="lm",
se=FALSE, size=1, color="black")

# Add axes labels and the plot title


g + geom_point() + facet_grid(drv~cyl, margins=TRUE) + geom_smooth(method="lm",
se=FALSE, size=1, color="black") + labs(x="Displacement", y="Highway Mileage",
title="Swirl Rules!")

You might also like