Table of Contents

Graphics in R

You can produce different kinds of graphs and visualizations in R (you can find creative examples here). However, I mostly use Excel to produce graphs because it is easier to use. But you may want to use some basic visualizations to examine the data. Here are some ways to create basic graphs in R. For histograms and Q-Q plots, please see a page for how to check the normality of data.


Bar plot

A bar plot (bar graph) is a most basic graph and it is probably your first choice when you want to visualize the means of different groups. Making a bar plot is easy in R. First, we prepare the data.

mean_data = c(13, 28, 21, 16, 19) sd_data = c(3, 5, 7, 4, 9) sample_size = 10 bar_name = c("A", "B", "C", "D", "E") group_size = length(mean_data)

Then, we can run the command for drawing a bar plot.

barplot(mean_data, names.arg=bar_name, col=c("red", "pink", "green", "lightblue", "blue"), ylim=c(0,35))

names.arg is the list of the names for groups, col is the list of the colors to be used, and ylim is the range of the values on the y axis. There are many other options for barplot(), and you can check them by running ?barplot.

Although making a bar plot is easy, adding an error bar is not trivial. Here, we are adding the 95% confidence interval for each bar. Basically, you have to calculate where the errors should be drawn manually.

ci_data = 1.96 * sd_data / sqrt(sample_size) error_x = (0.2 + 1) * 1:group_size - 1/2 arrows(error_x, mean_data, error_x, mean_data + ci_data, angle=90) arrows(error_x, mean_data, error_x, mean_data - ci_data, angle=90)

If you change the width of the bar, remember that you have to change the calculation of error_x.


Box plot

A box plot is a useful graph to know the distributions of different groups visually. Let's take a look at the example. First, we create data.

Device = factor(c(rep("Pen",24), rep("Touch",24))) Technique = factor(rep(c(rep("A",8), rep("B",8), rep("C",8)), 2)) Time = c(1.2,1.4,1.8,2.0,1.1,1.5,1.5,1.7, + 2.1,2.5,2.2,2.2,2.9,2.3,2.3,2.6, + 3.5,3.4,3.3,3.2,2.9,2.8,3.8,3.4, + 2.4,1.8,2.5,2.1,2.2,1.9,1.7,2.3, + 2.8,3.1,3.2,4.0,2.9,3.6,3.2,3.7, + 4.5,4.8,4.7,4.1,4.1,4.2,4.6,4.9)

Then, you can create a box plot by doing linear regression or ANOVA.

boxplot(Time ~ Technique)

You need to know what are displayed in this graph.

So, in this example, Group B looks a little skewed, and Group A and C look evenly distributed (so probably close to the normal distribution).

You can also specify multiple factors.

boxplot(Time ~ Device * Technique)


Multiple dot/line plot

If you have multiple groups of data over period (e.g., the number of experimental sessions), you want to draw a dot or line plot with multiple groups. And you can do it with matplot() in R. Here is an example of how to use matplot(). As usual, we first prepare the data.

x = 1:10 A = c(15, 36, 54, 60, 68, 71, 73, 75, 78, 78) B = c(20, 49, 58, 69, 75, 80, 83, 86, 88, 89) C = c(24, 58, 68, 75, 83, 90, 93, 93, 95, 96) Performance = data.frame(A,B,C)

Then, execute matplot().

> matplot(x,Performance, type="o", pch=c(1,2,3), col=c("red","green","blue"))

type represents the kind of graphs you want to create. If you do type=ā€œpā€, it will be a dot plot. If you do type=ā€œlā€, it will be a line plot without points. pch means the character to be used for plotting. If you specify an integer, it will be a shape (e.g., a square, a triangle, or a circle). col means the color to be used for each group. There are other options for matplot(), and you can see the help by doing ?matplot.