





















































Take control of your data and produce superior statistical analysis with R.
R features several options for creating charts, graphs, and plots. In this article, we will explore the generation and customization of these visuals, as well as methods for saving and exporting them for use outside of R. The following visuals will be covered in this article:
A bar chart or bar graph is a common visual that uses rectangles to depict the values of different items. Bar graphs are especially useful when comparing data over time or between diverse groups. Let us create a bar chart in R:
> #set the R working directory
> #replace the sample location with one that is relevant to you
> setwd("/Users/johnmquick/rBeginnersGuide/")
> #create a bar chart that compares the mean durations of
the battle methods
> #calculate the mean duration of each battle method
> meanDurationFire <- mean(subsetFire$DurationInDays)
> meanDurationAmbush <- mean(subsetAmbush$DurationInDays)
> meanDurationHeadToHead <-
mean(subsetHeadToHead$DurationInDays)
> meanDurationSurround <- mean(subsetSurround$DurationInDays)
> #use a vector to define the chart's bar values
> barAllMethodsDurationBars <- c(meanDurationFire,
meanDurationAmbush, meanDurationHeadToHead,
meanDurationSurround)
> #use barplot(...) to create and display the bar chart
> barplot(height = barAllMethodsDurationBars)
You created your first graphic in R. Let us examine the barplot(...) function that we used to generate our bar chart, along with the new R components that we encountered.
We created a bar chart that compared the mean durations of battles between the different combat methods. As it turns out, there is only one required argument in the barplot(...) function. This height argument receives a series of values that specify the length of each bar. Therefore, the barplot(...) function, at its simplest, takes on the following form:
barplot(height = heightValues)
Accordingly, our bar chart function reflected this same format:
> barplot(height = barAllMethodsDurationBars)
We stored the heights of our chart's bars in a vector variable. In R, a vector is a series of data. R's c(...) function can be used to create a vector from one or more data points. For example, the numbers 1, 2, 3, 4, and 5 can be arranged into a vector like so:
> #arrange the numbers 1, 2, 3, 4, and 5 into a vector
> numberVector <- c(1, 2, 3, 4, 5)
Similarly, text data can also be placed into vector form, so long as the values are contained within quotation marks:
> #arrange the letters a, b, c, d, and e into a vector
> textVector <- c("a", "b", "c", "d", "e")
Our vector defined the values for our bars:
> #use a vector to define the chart's bar values
> barAllMethodsDurationBars <- c(meanDurationFire, meanDurationAmbush, meanDurationHeadToHead, meanDurationSurround)
Many function arguments in R require vector input. Hence, it is very common to use and encounter the c(...) function when working in R.
When you executed your barplot(...) function in the R console, the graphic window opened to display it. The graphic window will have different names across different operating systems, but its purpose and function remain the same. For example, in Mac OS X, the graphic window is named Quartz.
For the remainder of this article, all R graphics will be displayed without the graphics window frame, which will allow us to focus on the visuals themselves.