Fundamental Graphics in R Prof. Ke-Sheng Cheng Dept. of Bioenvironmental Systems Eng. National...
-
Upload
susanna-gallagher -
Category
Documents
-
view
216 -
download
2
Transcript of Fundamental Graphics in R Prof. Ke-Sheng Cheng Dept. of Bioenvironmental Systems Eng. National...
Fundamental Graphics in R
Prof. Ke-Sheng Cheng
Dept. of Bioenvironmental Systems Eng.
National Taiwan University
Histogram
• hist(x, freq=FALSE, breaks=…)– breaks=• a vector giving the breakpoints between histogram cells, • a single number giving the number of cells for the
histogram.
– freq=• If TRUE, the histogram graphic is a representation of
frequencies, the counts component of the result,• if FALSE, probability densities, component density, are
plotted (so that the histogram has a total area of one).
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
2
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
3
Empirical CDF, ecdf
04/21/23 4Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
• The ecdf in R is a function.
04/21/23 5Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
04/21/23 6Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
04/21/23 7Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
Sample quantiles
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
8
Linear interpolation
Using the quantile function to calculate sample quantiles
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
9
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
10
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
11
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
12
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
13
Not linear interpolation!
These three numbers define the box. Whiskers are defined differently.
The QQ plots
• The quantile-quantile plots are a type of scatter plot used to compare distributions of two groups or to compare a sample with a reference distribution.
• When the groups are of different sizes, R reduces the size of the larger group to the size of the smaller one by keeping the minimum and maximum values, and choosing equally spaced quantiles between.
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
14
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
15
The boxplot in R• boxplot(x,range=0)• boxplot(x) [Default, range=1.5]• boxplot(x,range=3)• A box-and-whisker plot includes two major parts – the
box and the whiskers.• The parameter range determines how far the plot whiskers extend out from the box. If range is positive, the whiskers extend to the most extreme data point which is no more than range times the interquartile range from the box. A value of zero causes the whiskers to extend to the data extremes.
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
16
•
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
17
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
18
Comparison of multiple boxplots
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
19
Can also useboxplot(x1,x2,x3,names=c(“x1”, ”x2”, ”x3”))
• Comparison of multiple boxplots
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
20
Low-level graphics
• In R graphics, the display is divided into:– The plot region where data will be drawn– Four margin areas, numbered clockwise from 1 to 4,
starting at the bottom.
• After establishing the plot region and margins, we can start adding points, lines, polygons, and symbols to the plot region.
04/21/23Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
21
04/21/23Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
22
• Graphic components– points(x,y, …)– lines(x,y, …)– text(x,y, labels, …)– abline(a,b, …) # adds the line y=a+bx– abline(h=y, …)– abline(v=x, …)– polygon(x,y, …)– segments(x0,y0,x1,y1, …)– arrows(x0,y0,x1,y1, …)– symbols(x,y, …)– legend(x,y, legend, …)
04/21/23Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
23
04/21/23Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
24
04/21/23Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
25
Saving Graphs to Files
• The R graphics display can consists of various graphics devices. The default device is the screen. However, it is also possible to save a graph to a file by assigning other graphics devices.
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
26
• Assigning R graphics device – a pdf file
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
27
Use > jpg(“filename.jpg”), if you want to save a graph to a jpeg file.
Random number generation in R• R commands for stochastic simulation (for
normal distribution – pnorm – cumulative probability– qnorm – quantile function– rnorm – generating a random sample of a specific
sample size– dnorm – probability density function
For other distributions, simply change the distribution names. For examples, (punif, qunif, runif, and dunif) for uniform distribution and (ppois, qpois, rpois, and dpois) for Poisson distribution.
04/21/23 28Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
Approximation of the Poisson distribution by normal distribution
Demonstration using stochastic simulation
• Using R
)20,20()20( 2 NPoisson
131776.08682238.0120
20251]25[
XP
.
Estimated by normal approximation of Poisson distribution
04/21/23 29Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
Poisson CDF by stochastic simulation
Estimated by stochastic simulation of Poisson distribution
Direct calculation using theoretical CDF of Poisson distribution.
04/21/23 30Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
Approximation by normal distribution
Poisson CDF by stochastic simulation
04/21/23 31Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
3-D Graphics
04/21/23 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.
32