V5 Flashcards
Plots Plots Plots
how to define your own simple object with class
#first define the object - needs to be list my object add your own new class class(myobject)
how to code a custom print
print.myclass
how to overload a plot (code)
when error “do not know how to / cannot coerce” pops up
- plot.myclass
whats the difference when you overload an image instead of a plot
- doesn’t work on list, only matrix
whats the plotting philosophy and why is it good/bad?
“Artist’s palette” model
- start with blank canvas and build up from there
- start with plot function (or similar)
- use annotation functions to add/modify (text, lines, points, axis)
- convenient, mirrors how we think of building plots and analysing data
- can’t go back once plot has started (i.e. to adjust margins); need to plan in advance
- difficult to “translate to others once a new plot has been created(no graphical “language”)
- Plot is just a series of R commands
important plotting parameters
pch - plotting symbol
lty - line type
lwd - line width
col - plotting color
xlab - character string for the x-axis label
ylab - character string for the y-axis label
what is the par() function
used to specify global graphics parameters that affect all plots in an R session. These parameters can be overridden when specified as arguments to specific plotting functions
important parameters of the par() function
las - orientation of the axis labels on the plot
bg - the background colour
mar - margin size
oma - outer margin size
mfrow - number of plots per row, column (plots are filled row-wise)
mccollum - number of plots period, column (plots are filled column wise)
how to get current par settings of colour (example)
par(“col”)
[1] “black”
functions that are vital for plotting (7)
plot - make a scatterplot, or other type of plot depending on the class of the object being plotted
lines - add lines to a plot, given a vector x values and a corresponding vector of y values (or a 2-column matrix); this function just connects the dots)
points - add points to a plot
text - add text labels to a plot using specified x, y coordinates
title - add annotations to x, y axis labels, title, subtitle, outer margin (plots outside of plot window)
mtext - add arbitrary text to the margins (inner or outer) of the plot
axis - adding axis tracks / labels
colour and ranges
for example: rainbow , heat.colors, etc
gray()
how to code transparency
rgb(red, green, blue, alpha) -> alpha = 1 max visible , alpha = 0 minimum visible
the with() function
- because plots can become bulky
- convenience function
- turns columns of data.frames into variables
- this is why read.table has check.names
mydata[,”Ozone”]
with(my data,{
mean(Ozone)
})
-> to get the mean of the column
how to build up a plot (code) # plot all wind zone per ozone in Ny with header title
highlight within plot month 5 data in blue and all other read
add legend
plot all wind zone per ozone in Ny with header title
with(air quality,
plot(Wind, Ozone,
main = “Ozone and Wind in NY”))
highlight within plot month 5 data in blue and other blue
with(subset(air quality, Month == 5),
points(Wind, Ozone, col = “blue”))
with(subset(air quality, Month != 5),
points(Wind, Ozone, col = “red”))
add legend
legend(“topright”, pch = 1, col = c(“blue”, “red”), legend = c(“May”, “Other Months”))
how to add a best fit line (code)
# y = a + bx model