06 Plots Export Plots
06 Plots Export Plots
06 Plots Export Plots
graphic files
To remember: ToDo:
Basic operators Plots
Variable assignment
Basic data types Exporting plots
Vectors
Matrices
Row and column names
Indexing a matrix
Lists
Data frames
Time series
Arrays
Read and writ data
How do you read and write data
in R?
The read.table() function
The scan() function - primitive input function
The write.table() function
dec =“,”
header
ToDo 1.
Suppose that your data is saved as a csv file (csv stand for comma-separated
values). We need to read the data into R.
Consider an experiment where the water level of Amazon was measured every
year. The data are saved in the file Water_levels.csv
1. Open the file Water_levels.csv in Excel (or a similar application), and make sure
you understand the structure of the file.
2. Read the data into R, use the command read.csv
3. Plot the Amazon water level
4. Calculate the mean, min and max of water level
5. Extract the first column and them the second row from the data
How do you calculate which seller sells more chocolate with nuts and
which uses more ingredients?
plot(choko2$Vendor, choko2$Number.of.Ingredients)
plot(choko2$Vendor, choko2$nuts)
R plotting
Source: W. N. Venables, D. M. Smith, An Introduction to R, Notes on R: A Programming Environment for Data Analysis and Graphics Version 3.5.0 Under development (2017-10-16)
The plot() function - is a generic function: the type of plot produced is dependent on
the type or class of the first argument.
plot(x, y)
plot(xy) #If x and y are vectors, plot(x, y) produces a scatterplot of y against x. The
same effect can be produced by supplying one argument (second form) as either a list
containing two elements x and y or a two-column matrix.
plot(x) #If x is a time series, this produces a time-series plot. If x is a numeric
vector, it produces a plot of the values in the vector against their index in the
vector. If x is a complex vector, it produces a plot of imaginary versus real parts of
the vector elements.
plot(f)
plot(f, y) #f is a factor object, y is a numeric vector. The first form generates a bar
plot of f ; the second form produces boxplots of y for each level of f.
plot(df)
plot(~ expr)
plot(y ~ expr)
df is a data frame, y is any object, expr is a list of object names separated by ‘+’
(e.g., a + b + c). The first two forms produce distributional plots of the variables in a
data frame (first form) or of a number of named objects (second form). The third
form plots y against every object named in expr.
Source: W. N. Venables, D. M. Smith, An Introduction to R, Notes on R: A Programming Environment for Data Analysis and Graphics Version 3.5.0 Under development (2017-10-16)
2 Low-level plotting commands
Sometimes the high-level plotting functions don’t produce exactly the kind of plot
you desire.
In this case, low-level plotting commands can be used to add extra information
(such as points, lines or text) to the current plot.
Some of the more useful low-level plotting functions are:
points(x, y)
lines(x, y)
Adds points or connected lines to the current plot. plot()’s type= argument can
also be passed to these functions (and defaults to "p" for points() and "l" for
lines().)
text(x, y, labels, ...)
Add text to a plot at points given by x, y. Normally labels is an integer or
character vector in which case labels[i] is plotted at point (x[i], y[i]). The default
is 1:length(x).
Source: W. N. Venables, D. M. Smith, An Introduction to R, Notes on R: A Programming Environment for Data Analysis and Graphics Version 3.5.0 Under development (2017-10-16)
3. Interactive graphics functions
There are several packages that offer interactive graphics for their specific purpose,
they often require the installation of additional libraries inside and software libraries
outside R, but these packages are self-contained, as R has not been conceived for
interactive graphics.
(Source: http://www.unige.ch/ses/sococ/cl/r/intergraph.e.html)
E.g. a ggplot2 chart can be made interactive using Ggplotly function and 3D surface
plots in R can be made using library(plotly)
packageVersion('plotly'). Lettice package: e.g. xyplot() useful for scatterplots and
time-series plots
http://www.r-graph-gallery.com/2017/06/07/get-the-best-from-ggplotly/ https://plot.ly/r/3d-surface-plots/
High-Level Plot Functions
Some of the basic plot functions include:
Source:
https://apps.carleton.edu/curricular/math/assets/RPlot.pdf
List of colors in R : http://
research.stowers.org/mcm/efg/R/Color/Chart/ColorChart.pdf
Or colors() # list of all named colors in R
Example:
#Color Basics: Points
x <- -2:2
y <- x^2
plot(x,y, col=1:5, pch=CIRCLE<-16, cex=2)
Source: http://research.stowers.org/mcm/efg/Report/UsingColorInR.pdf
Exporting plots as graphic files
Steps: 1.
setwd("Y:/.....................")
2.
dev.cur() # the graphics device we're currently writing to
here: the built-in Rstudio device
3.
pdf(file = "plot1.pdf") # opening a pdf device
dev.cur() # currently active device is now pdf
dev.off() # closes the active device and displays the new
current device
dev.cur() # currently active device is now Rstudio again
South Africa https://pixabay.com/en/south-africa-landscape-scenic-sky-1982418/
ToDo1
# Let's start with some very simple example data:
# "beavers" is one of the data sets included in R
data() # list all built-in example data sets
data(beavers) # load "beavers" data
ls() # there are two new objects in our workspace: "beaver1" and "beaver2"
?beavers # info on the data set
# have a look at the data
str(beaver1) # structure
str(beaver2)
#have a look at the head and the tail of the data, function: head(), tail()
…..........
# create subsets of the data set containing only the data for the first day covered
beaver1 <- subset(beaver1, beaver1$day==346)
beaver2 <- …………………………………….
###################
# actual plotting for beaver1
#plot only the temperature: Y-axis shows row number of each temperature value
……………..
#plot temperature against time (you can use ~ or , )
…………….
# Set axis labels and Title as:
y-axis label: “Body Temperature in Celsius”, x-axis label: “Time of Day”, title: “Beaver 1”
………………………….
#Change the type to line
………………………….
# customize your plots - change the plot symbol to triangle and color blue ( ?points )
…………………….
# changing colors
#change the color to "#000000“ and the background (fill) to "#FFA500“
…………………….
# size of various plot elements - duplicate symbol size
……………………...
# slightly increase axis annotations
………………………………
# slightly decrease axis labels e.g. cex.lab=0.7 (?par # more info on graphical parameters)
……………………….
# changing lines (?par # more options)
plot(temp ~ time, data=beaver1, type="l")
#change the line width to 4
………………….
#make a dashed line
………………….
# changing box type- suppress the box (?par # more info on graphical parameters)
……………………….
ToDo2
Make a multi-panel plots (use function par() and mfrow)
e.g. op <- par(mfrow = c(2, 2), # 2 x 2 pictures on one plot
Plot 1 row and 2 columns
par(…………….
plot(temp ~ time, data=beaver1, main="Beaver 1")
plot(temp ~ time, data=beaver2, main="Beaver 2")
e.g.
pdf(file = "plot2.pdf") # opening another pdf device
plot(temp ~ time, data=beaver1) # plot data to this device
dev.off() # don't forget to close the device!
#Customize your plot (as you like: color, box type) and save it as a pdf file with
width = 7 and height = 5
……………………….
#Export the plot as a PNG file with width=800, height=600 and units = "px“
………………………
Karoo landscape near the Cockscomb mountains in the Eastern Cape of South Africa http://tandoguzana.com/environment/
Resources:
W. N. Venables, D. M. Smith, An Introduction to R, Notes on R: A Programming Environment
for Data Analysis and Graphics Version 3.5.0 Under development (2017-10-16)
Customizing your plots in R https://www.youtube.com/watch?v=0MrYVzPxBIc&t=23s
Data visualization in R
How to plot multiple graphs in R https://www.youtube.com/watch?v=Z3V4Pbxeahg&t=14s
Install packages in R: https://www.datacamp.com/community/tutorials/r-packages-guide