# 8.3: Plotting...

$$\newcommand{\vecs}{\overset { \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

At the beginning, visually check the distribution of data. Make histogram:

Code $$\PageIndex{1}$$ (R):

data <- read.table("data/bugs.txt", h=TRUE)
hist(data$WEIGHT, breaks=3) (To see more detailed histogram, increase the number of breaks.) If for the histogram you want to split data in the specific way (for example, by 20 units, starting from 0 and ending in 100), type: Code $$\PageIndex{2}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) hist(data$WEIGHT, breaks=c(seq(0, 100, 20))

Boxplots show outliers, maximum, minimum, quartile range and median for any measurement variable:

Code $$\PageIndex{3}$$ (R):

data <- read.table("data/bugs.txt", h=TRUE)
boxplot(data$LENGTH) ... now for males and females separately, using formula interface: Code $$\PageIndex{4}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) boxplot(data$LENGTH ~ data$SEX) There are two commands which together help to check normality of the character: Code $$\PageIndex{5}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) qqnorm(data$WEIGHT); qqline(data$WEIGHT) (These two separate commands work together to make a single plot, this is why we used semicolon. The more dots on the resulting plot are deviated from the line, the more non-normal is the data.) Make scatterplot where all bugs represented with small circles. X axis will represent the length whereas Y axis—the weight: Code $$\PageIndex{6}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) plot(data$LENGTH, data$WEIGHT, type="p") (type="p" is the default for plot(), therefore it is usually omitted.) It is possible to change the size of dots varying the cex parameter. Compare Code $$\PageIndex{7}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) plot(data$LENGTH, data$WEIGHT, type="p", cex=0.5) with Code $$\PageIndex{8}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) plot(data$LENGTH, data$WEIGHT, type="p", cex=2) How to compare? The best way is to have more than one graphical window on the desktop. To start new window, type dev.new(). It is also possible to change the type of plotting symbol. Figure $$\PageIndex{1}$$ shows their numbers. If you want this table on the computer, you can run: Code $$\PageIndex{9}$$ (R): Ex.points() # gmoon.r Figure $$\PageIndex{1}$$ Point types in R standard plots. For types 21–25, it is possible to specify different background and frame colors. To obtain similar graphic examples about types of lines, default colors, font faces and plot types, load the gmoon.r$$^{}$$ and run: Code $$\PageIndex{10}$$ (R): Ex.lines() # gmoon.r Ex.cols() # gmoon.r Ex.fonts() # gmoon.r Ex.types() # gmoon.r Use symbol 2 (empty triangle): Code $$\PageIndex{11}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) plot(data$LENGTH, data$WEIGHT, type="p", pch=2) Use text codes (0/1) for the SEX instead of graphical symbol: Code $$\PageIndex{12}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) plot(data$LENGTH, data$WEIGHT, type="n") text(data$LENGTH, data$WEIGHT, labels=data$SEX)

(Here both commands make one plot together. The first one plots the empty field with axes, the second add there text symbols.)

The same plot is possible to make with the single command, but this works only for one-letter labels:

Code $$\PageIndex{13}$$ (R):

data <- read.table("data/bugs.txt", h=TRUE)
plot(data$LENGTH, data$WEIGHT, pch=as.character(data$SEX)) If we want these numbers to have different colors, type: Code $$\PageIndex{14}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) plot(data$LENGTH, data$WEIGHT, type="n") text(data$LENGTH, data$WEIGHT, labels=data$SEX, col=data$SEX+1) (Again, both commands make one plot. We added +1 because otherwise female signs would be of 0 color, which is “invisible”.) Different symbols for males and females: Code $$\PageIndex{15}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) plot(data$LENGTH, data$WEIGHT, type="n") points(data$LENGTH, data$WEIGHT, pch=data$SEX)

The more complicated variant—use symbols from Hershey fonts$$^{}$$ which are internal in R(Figure $$\PageIndex{2}$$):

Code $$\PageIndex{16}$$ (R):

data <- read.table("data/bugs.txt", h=TRUE)
plot(data$LENGTH^3, data$WEIGHT, type="n", xlab=expression("Volume (cm"^3*")"), ylab="Weight")
text(data$LENGTH^3, data$WEIGHT, labels=ifelse(data$SEX, "\MA", "\VE"), vfont=c("serif","plain"), cex=1.5) (Note also how expression() was employed to make advanced axes labels. Inside expression(), different parts are joined with star *. To know more, run ?plotmath.) Figure $$\PageIndex{2}$$ Distribution of male and female bugs by size and weight (Hershey fonts used) We can paint symbols with different colors: Code $$\PageIndex{17}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) plot(data$LENGTH, data$WEIGHT, type="n") points(data$LENGTH, data$WEIGHT, pch=data$SEX*3, col=data$SEX+1) Finally, it is good to have a legend: Code $$\PageIndex{18}$$ (R): legend("bottomright", legend=c("male", "female"), pch=c(0, 3), col=1:2) And then save the plot as PDF file: Code $$\PageIndex{19}$$ (R): dev.copy(pdf, "graph.pdf") dev.off() Attention! Saving into the external file, never forget to type dev.off()! If you do not want any of axis and main labels, insert options main="", xlab="", ylab="" into your plot() command. There is also a better way to save plots because it does not duplicate to screen and therefore works better in R scripts: Code $$\PageIndex{20}$$ (R): data <- read.table("data/bugs.txt", h=TRUE) pdf("graph.pdf") plot(data$LENGTH, data$WEIGHT, type="n") points(data$LENGTH, data$WEIGHT, pch=data$SEX*3, col=data\$SEX+1)
legend("bottomright", legend=c("male", "female"), pch=c(0, 3), col=1:2)
dev.off()

(Please note here that R issues no warning if the file with the same name is already exist on the disk, it simply erases it and saves the new one. Be careful!)

## References

1. With command source("ashipunov.info/r/gmoon.r").

2. To know which symbols are available, run demo(Hershey).