The plot() function can be used to create basic plots in R. Plots generated in RStudio will display in the ‘Plots’ panel. The window size can be increased to increase the plot size or decreased to decrease the plot size.
The type of plot that is produced will depend on the type of data provided to the function. In the examples that follow, we will use the store.df dataframe, which we import from a csv file on a website. We set stringsAsFactors = TRUE to import variables (columns) with character values as factor variables.
store.df <- read.csv("http://goo.gl/QPDdMl",
stringsAsFactors = TRUE)
If a factor (categorical) variable is provided as the input argument x, a barplot will be the output.
plot(x = store.df$country)
If two numerical variables are provided either as the x and y arguments or a formula y ~ x, the plot will be a scatterplot.
plot(x = store.df$p1sales, # x-axis variable
y = store.df$p2sales, # y-axis variable
xlab = "P1 Sales", # x-axis label
ylab = "P2 Sales") # y-axis label
If a factor variable is provided as the x argument and a numeric variable as the y argument, the plot will be a grouped boxplot.
plot(formula = p1sales ~ country,
data = store.df)
The xlab and ylab arguments can be used to update the x and y axis labels. The main argument can be used to add a plot title. The col argument can add color to the plot.
plot(formula = p1sales ~ country,
data = store.df,
xlab = "Country",
ylab = "P1 Sales",
main = "P1 Sales by Country", # title
col = "hotpink") # color
R currently has 16,657 packages available to use. To view packages that you have already installed and ready for use, you can use the library() function with no arguments specified. Once you have installed a package, it is ready for use. You only need to install a package using the install.packages() function once. You can install more than one package at a time using the install.packages() function.
install.packages("cowsay")
Once a package is installed, you can load it for use in your current RStudio session using the library() function. You must load a package in each RStudio session that you want to use the package. You can only load one package per library() function.
library(cowsay)
To obtain help documentation for a package, you can use ?? or the help() function, specifying the package name in the package argument.
help(package = "cowsay")
Per the documentation, the only function is say(), which can be used to “sling messages and warnings with flair”.
args(say)
## function (what = "Hello world!", by = "cat", type = NULL, what_color = NULL,
## by_color = NULL, length = 18, fortune = NULL, ...)
## NULL
Since all arguments have default values, there are no required arguments to run the function.
say()
##
## --------------
## Hello world!
## --------------
## \
## \
## \
## |\___/|
## ==) ^Y^ (==
## \ ^ /
## )=*=(
## / \
## | |
## /| | | |\
## \| | |_|/\
## jgs //_// ___/
## \_)
##
Based on the help documentation, the say() function can be called with any character string, a range of animals, colors, etc.
say(what = "STAT 331 is my favorite class ever!",
by = "cow")
##
## -----
## STAT 331 is my favorite class ever!
## ------
## \ ^__^
## \ (oo)\ ________
## (__)\ )\ /\
## ||------w|
## || ||
Some packages that we will use (and you should install!) are: cluster, caret, DescTools, ggplot2, fpc, factoextra, gridExtra, arules, arulesViz, class, e1071, rpart, rpart.plot, nnet, neuralnet, randomForest, ipred, caretEnsemble, DMwR, tm, wordcloud, SnowballC