Fundamentals 11 min read

Master R Data Visualization: Essential Techniques for Clear Graphs

This guide explains why data visualization matters, how to create and save graphics in R, adjust graphical parameters, and use core plotting functions such as barplot, pie, hist, boxplot, and scatter plots to produce clear, informative charts.

Model Perspective
Model Perspective
Model Perspective
Master R Data Visualization: Essential Techniques for Clear Graphs

Data visualization uses graphical methods to present data, enabling clear and effective communication. It makes abstract numbers intuitive, reveals patterns that are hard to detect otherwise, and lowers the barrier for audiences without specialized data science knowledge. R offers a powerful graphics system with many packages for this purpose.

General Methods for Drawing Graphics

(1) Creating and Saving Graphics In R, graphics can be saved by placing plotting commands between device‑opening and device‑closing calls. Functions such as pdf() , png() , jpeg() , bmp() , tiff() , xfig() , postscript() start a graphics device, and dev.off() closes it. Multiple windows can be opened with dev.new() , dev.next() , dev.prev() , dev.set() . Some packages require print() to write the plot to a file. On Windows, the GUI allows “File → Save As…”, while RStudio provides an Export button; on Unix/Linux, graphics must be saved via code.

(2) Setting Graphic Parameters The par() function modifies global graphic parameters such as fonts, colors, axes, and titles. Calling par() without arguments returns a list of current settings; adding no.readonly=TRUE makes the list editable. Parameters control symbols, line types, colors, text attributes, and dimensions. Advanced plotting functions like plot() , hist() , and boxplot() also accept axis and label options. Additional functions include title() (add titles/labels), axis() (custom axes), abline() (reference lines), legend() (legends), text() and mtext() (add text inside or at margins).

Layout can be arranged with par(mfrow=c(nrows,ncols)) for row‑wise matrices or par(mfcol=c(nrows,ncols)) for column‑wise matrices. The fig parameter and layout(mat) function provide finer control, with widths and heights adjusting individual plot sizes.

Bar Plot

The barplot(height, xlab, ylab, main, names.arg, col, beside) function creates vertical or horizontal bar charts. height is a vector or matrix of values; xlab and ylab label the axes; main sets the title; names.arg provides bar labels; col sets colors; beside determines whether bars are stacked (FALSE) or placed side‑by‑side (TRUE).

Pie Chart

The pie(x, labels=names(x), edges=200, radius=0.8, clockwise=FALSE, init.angle=if(clockwise) 90 else 0, density=NULL, angle=45, col=NULL, border=NULL, main=NULL, ...) function draws a pie chart, where x is a numeric vector of slice areas and labels provides slice labels. Additional arguments control edge smoothness, radius, direction, shading density, colors, and title. The plotrix package offers pie3D() for 3‑D pies after installing plotrix .

Histogram

The hist(x) function creates histograms, displaying the distribution of a continuous variable. Typical usage: hist(x, breaks="Sturges", xlab=..., col=..., freq=NULL, probability=..., main=...) . Parameters include x (numeric vector), breaks (binning strategy), xlab (x‑axis label), col (color), freq (TRUE for frequency, FALSE for density), probability (inverse of freq ), and main (title).

Boxplot

The boxplot() function visualizes the five‑number summary of a continuous variable, highlighting potential outliers. Basic syntax: boxplot(x, range=1.5, width=NULL, varwidth=FALSE, notch=FALSE, horizontal=FALSE, ...) . Arguments control the data vector, whisker length ( range ), box width, proportional width ( varwidth ), notch display, and orientation. For grouped data, use boxplot(formula, data=dataframe) , where formula specifies the response and grouping variable (e.g., y ~ A ).

Scatter Plot

The plot() function creates scatter plots, showing relationships between two variables. Syntax: plot(x, y, type="p", main, xlab, ylab, xlim, ylim, axes) . type options include p (points), l (lines), o (points and lines), h (vertical lines), s (stepwise), and S (reverse stepwise). Additional arguments set titles, axis labels, limits, and whether axes are drawn. The pairs() function creates a scatter‑plot matrix for multiple variables, using pairs(formula, data) .

Source: Liu Hongde, Sun Xiao, Xie Jianming, "Biological Data Analysis and Practice".

graphicsstatisticsData VisualizationchartsPlottingR
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.