V5 Flashcards

Plots Plots Plots

1
Q

how to define your own simple object with class

A
#first define the object - needs to be list
my object  add your own new class
class(myobject)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to code a custom print

A

print.myclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to overload a plot (code)

A

when error “do not know how to / cannot coerce” pops up

- plot.myclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

whats the difference when you overload an image instead of a plot

A
  • doesn’t work on list, only matrix
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

whats the plotting philosophy and why is it good/bad?

A

“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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

important plotting parameters

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is the par() function

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

important parameters of the par() function

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how to get current par settings of colour (example)

A

par(“col”)

[1] “black”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

functions that are vital for plotting (7)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

colour and ranges

A

for example: rainbow , heat.colors, etc

gray()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how to code transparency

A

rgb(red, green, blue, alpha) -> alpha = 1 max visible , alpha = 0 minimum visible

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

the with() function

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
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

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to add a best fit line (code)

A
# y = a + bx
model
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to make multiple base plots

A
  • only if variable names are valid
- par (mfrow = c(1,3), oma = c(0,0,2,0)){
#all the plots you are gonna code
}

oma for space between plots

17
Q

how to code a pie chart with percentages

A

slices

18
Q

hot to code a dendrogram

  • labels at same level
A
# prepare hierarchical closer
hc = hclust(dist(mtcars))
-> dist to calculate distance, hclust to cluster those distances
#very simple dendrogram
plot(hc)
# labels at the same level
plot(hc, hand = -1)
19
Q

how to plot a dendrogram in more ways

A
# create a dendrogram object
hcd = as.dendrogram(hc)
20
Q

four different phylogenic trees + library

how to code

A

library(ape)

#plot basic tree
plot(as.phylo(hc), cex = 0.9, label.offset = 1)
# cladogram
plot(as.phylo(hc), type = "cladogram", cex = 0.9, label.offset = 1)
# unrooted
plot(as.phylo(hc), type = "unrooted")
#fan
plot(as.phylo(hc), type = "fan"