From no-frills graphics to adding color and labeling your data, here’s what you need to know.
One of the most appealing things about R is its ability to create data visualizations with just a couple of lines of code.
plot (mtcars$disp, mtcars$mpg)
You really can’t get much easier than that.
Of course that’s a pretty no-frills graphic. If you’d like to label your x and y axes, use the parameters xlab and ylab. To add a main headline, such as „Page views by time of day, “ use the parameter main:
plot (mtcars$disp, mtcars$mpg, xlab=“Engine displacement“, ylab=“mpg“, main=“MPG compared with engine displacement“)
If you find having the y-axis labels rotated 90 degrees annoying (as I do) , you can position them for easier reading with the las=1 argument:
plot (mtcars$disp, mtcars$mpg, xlab=“Engine displacement“, ylab=“mpg“, main=“MPG vs engine displacement“, las=1)
?par
In addition to the basic dataviz functionality included with standard R, there are numerous add-on packages to expand R’s visualization capabilities. Some packages are for specific disciplines such as biostatistics or finance; others add general visualization features.
Why use an add-on package if you don’t need something discipline-specific? If you’re doing more complex dataviz, or want to pretty up your graphics for presentations, some packages have more robust options. Another reason: The organization and syntax of an add-on package might appeal to you more than do the R defaults.
In particular, the ggplot2 package is quite popular and worth a look for robust visualizations. ggplot2 requires a bit of time to learn its „Grammar of Graphics“ approach .