Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Graphics in R Prof. Ke-Sheng Cheng Dept. of Bioenvironmental Systems Eng. National Taiwan University.

Similar presentations


Presentation on theme: "Fundamental Graphics in R Prof. Ke-Sheng Cheng Dept. of Bioenvironmental Systems Eng. National Taiwan University."— Presentation transcript:

1 Fundamental Graphics in R Prof. Ke-Sheng Cheng Dept. of Bioenvironmental Systems Eng. National Taiwan University

2 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). 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 2

3 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 3

4 Empirical CDF, ecdf 9/9/2015 4 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.

5 The ecdf in R is a function. 9/9/2015 5 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.

6 9/9/2015 6 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.

7 9/9/2015 7 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.

8 Sample quantiles 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 8 Linear interpolation

9 Using the quantile function to calculate sample quantiles 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 9

10 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 10

11 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 11

12 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 12

13 9/9/2015 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.

14 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. 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 14

15 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 15

16 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. 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 16

17 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 17

18 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 18

19 Comparison of multiple boxplots 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 19 Can also use boxplot(x1,x2,x3,names=c(“x1”, ”x2”, ”x3”))

20 Comparison of multiple boxplots 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 20

21 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. 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 21

22 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 22

23 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, …) 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 23

24 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 24

25 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 25

26 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. 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 26

27 Assigning R graphics device – a pdf file 9/9/2015 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.

28 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. 9/9/2015 28 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.

29 Approximation of the Poisson distribution by normal distribution Demonstration using stochastic simulation Using R. Estimated by normal approximation of Poisson distribution 9/9/2015 29 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.

30 Poisson CDF by stochastic simulation Estimated by stochastic simulation of Poisson distribution Direct calculation using theoretical CDF of Poisson distribution. 9/9/2015 30 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.

31 Approximation by normal distribution Poisson CDF by stochastic simulation 9/9/2015 31 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ.

32 3-D Graphics 9/9/2015 Laboratory for Remote Sensing Hydrology and Spatial Modeling, Dept of Bioenvironmental Systems Engineering, National Taiwan Univ. 32


Download ppt "Fundamental Graphics in R Prof. Ke-Sheng Cheng Dept. of Bioenvironmental Systems Eng. National Taiwan University."

Similar presentations


Ads by Google