In this chapter , we use a broad range of simulations and hands-on activities to highlight some of the basic data visualization techniques using R. A brief discussion of alternative visualization methods is followed by demonstrations of histograms , density, pie, jitter, bar, line and scatter plots, as well as strategies for displaying trees, more general graphs, and 3D surface plots. Many of these are also used throughout the textbook in the context of addressing the graphical needs of specific case-studies.

It is practically impossible to cover all options of every different visualization routine. Readers are encouraged to experiment with each visualization type, change input data and parameters, explore the function documentation using R-help (e.g., ? plot ), and search online for new R visualization packages and new functionality, which are continuously being developed.

We will cover (1) one specific classification of visualization methods, (2) composition (e.g., density, histogram), comparison (e.g., jitter, bar, correlation) and relationship (e.g., line) plots, (3) 2D kernel density and 3D surface plots, and (4) 3D and 4D visualization of solids, (hyper)volumes.

4.1 Common Questions

  • What exploratory visualization techniques are available to graphically interrogate my specific data?

  • How do we examine paired associations and correlations in a multivariate dataset?

4.2 Classification of Visualization Methods

Scientific data-driven or simulation-driven visualization methods are hard to classify. The following list of criteria can be used to characterize alternative data visualization strategies:

  • Data Type: structured/unstructured, small/large, complete/incomplete, time/space, ASCII/binary, Euclidean/non-Euclidean, etc.

  • Task type: Task type is one of the aspects considered in classification of visualization techniques, which provides a means of interaction between the researcher, the data, and the display software/platform

  • Scalability: Visualization techniques are subject to some limitations, such as the amount of data that a particular technique can exhibit

  • Dimensionality: Visualization techniques can also be classified according to the number of attributes

  • Positioning and Attributes: the distribution of attributes on the chart may affect the interpretation of the display representation, e.g., correlation analysis, where the relative distance among the plotted attributes is relevant for observation

  • Investigative Need: the specific scientific question or exploratory interest may also determine the type of visualization:

    • Examining the composition of the data

    • Exploring the distribution of the data

    • Contrasting or comparing several data elements, relations, association

    • Unsupervised exploratory data mining.

Also, we have the following table for common data visualization methods according to task types (Fig. 4.1):

Fig. 4.1
figure 1

Schematic depiction of a hierarchical classification of different visualization methods

We introduce common data visualization methods according to this classification criterion, albeit this is not a unique or even broadly agreed upon ontological characterization of exploratory data visualization.

4.3 Composition

In this section, we will see composition plots for different types of variables and data structures.

4.3.1 Histograms and Density Plots

One of the first few graphs we learn is a histogram plot. In R, the command hist () is applied to a vector of values and used for plotting histograms . The famous nineteenth century statistician Karl Pearson introduced histograms as graphical representations of the distribution of a sample of numeric data. The histogram plot uses the data to infer and display the probability distribution of the underlying population that the data is sampled from. Histograms are constructed by selecting a certain number of bins covering the range of values of the observed process. Typically, the number of bins for a data array of size N should be equal to \( \sqrt{N} \). These bins form a partition (disjoint and covering sets) of the range. Finally, we compute the relative frequency representing the number of observations that fall within each bin interval. The histogram just plots a piece-wise step-function defined over the union of the bin interfaces whose height equals the observed relative frequencies (Fig. 4.2) .

figure a
Fig. 4.2
figure 2

Overlay of Normal distribution histogram and density curve plot

Here, freq=T shows the frequency for each x value and breaks controls for number of bars in our histogram.

The shape of the last histogram we drew is very close to a Normal distribution (because we sampled from this distribution by rnorm). We can add a density line to the histogram (Fig. 4.3).

figure b
Fig. 4.3
figure 3

Overlay of histogram plot and a density curve estimate

We used the option freq=F to make the y axis represent the “relative frequency”, or “density”. We can also use plot (density(x)) to draw the density plot by itself (Fig. 4.4).

figure c
Fig. 4.4
figure 4

Direct plot of the estimated Normal distribution density curve

4.3.2 Pie Chart

We are all very familiar with pie charts that show us the components of a big “cake”. Although pie charts provide effective simple visualization in certain situations, it may also be difficult to compare segments within a pie chart or across different pie charts. Other plots like bar chart, box or dot plots may be attractive alternatives.

We will use the Letter Frequency Data on SOCR website to illustrate the use of pie charts.

figure d
figure e

We can try to plot the frequency for the first ten English letters. The left hand side shows a table made by the function legend (Fig. 4.5).

figure f
Fig. 4.5
figure 5

Pie chart showing the frequency of English use of the first ten Latin letters (a–j)

The input type for pie () is a vector of non-negative numerical quantities. In the pie function, we list the data that we are going to use (positive and numeric), the labels for each of them, and the colors we want to use for each sector. In the legend function, we put the location in the first slot and use legend as the labels for the colors. Also cex, bty, pch, and pt.cex are all graphic parameters that we have talked about in Chap. 2.

More elaborate pie charts, using the Latin letter data, will be demonstrated using ggplot below (in the Appendix of this chapter ).

4.3.3 Heat Map

Another common data visualization method is the heat map. Heat maps can help us visualize intuitively the individual values in a matrix. It is widely used in genetics research and financial applications.

We will illustrate the use of heat maps, based on a neuroimaging genetics case-study data about the association (p-values) of different brain regions of interest (ROIs) and genetic traits (SNPs) for Alzheimer’s disease (AD) patients, subjects with mild cognitive impairment (MCI), and normal controls (NC). First, let’s import the data into R. The data are 2D arrays where the rows represent different genetic SNPs, columns represent brain ROIs, and the cell values represent the strength of the SNP-ROI association, a probability value (smaller p-values indicate stronger neuroimaging-genetic associations).

figure g

Then, we load the R packages we need for heat maps (use install.packages(" package name") first if you have not previously install them).

figure h

We can convert the datasets into matrices.

figure i

We may also want to set up the row (rc) and column (cc) colors for each cohort.

figure j

Finally, we can plot the heat maps by specifying the input type of heatmap () to be a numeric matrix (Figs. 4.6, 4.7, and 4.8) .

figure k
figure l
figure m
Fig. 4.6
figure 6

Hierarchically clustered heatmap for the Alzheimer’s disease (AD) cohort of the dementia study. The rows indicate the unique SNP reference sequence (rs) IDs and the columns index specific brain regions of interest (ROIs) that are associated with the genomic biomarkers (rows)

Fig. 4.7
figure 7

Hierarchically clustered heatmap for the Mild Cognitive Impairment (MCI) cohort

Fig. 4.8
figure 8

Hierarchically clustered heatmap for the healthy normal controls (NC) cohort

In the heatmap () function, the first argument provides the input matrix we want to use. col is the color scheme; scale is a character indicating if the values should be centered and scaled in either the row direction or the column direction, or none ("row", "column", and "none"); RowSideColors and ColSideColors creates the color names for horizontal side bars.

The differences between the AD, MCI, and NC heat maps are suggestive of variations of genetic traits or alternative brain regions that may be affected in the three clinically different cohorts.

4.4 Comparison

Plots used for comparing different individuals, groups of subjects, or multiple units represent another set of popular exploratory visualization tools.

4.4.1 Paired Scatter Plots

Scatter plots use the 2D Cartesian plane to display a pair of variables. 2D points represent the values of the two variables corresponding to the two coordinate axes. The position of each 2D point on is determined by the values of the first and second variables, which represent the horizontal and vertical axes. If no clear dependent variable exists, either variable can be plotted on the X axis and the corresponding scatter plot will illustrate the degree of correlation (not necessarily causation) between the two variables.

Basic scatter plots can be plotted by function plot (x, y) (Fig. 4.9).

figure n
Fig. 4.9
figure 9

Scatter plot of bivariate uniform process

qplot() is another way to display elaborate scatter plots. We can manage the colors and sizes of dots. The input type for qplot() is a data frame. In the following example, larger x will have larger dot sizes. We also grouped the data as ten points per group (Fig. 4.10).

figure o
Fig. 4.10
figure 10

Simulated bubble plot depicting four variable features represented as x and y axes, size and color

Now, let’s draw a paired scatter plot with three variables. The input type for pairs () function is a matrix or data frame (Fig. 4.11) .

figure p
Fig. 4.11
figure 11

A pairs plot depicts the bivariate relations in multivariate datasets

We can see that variable names are on the diagonal of this scatter plot matrix. Each plot uses the column variable as its X-axis and row variable as its Y-axis.

Let’s see a real word data example. First, we can import the Mental Health Services Survey Data into R, which is on the case-studies website.

figure q

From the head() output, we observe that there are a lot of NA ’s in the dataset. pairs automatically deals with this problem (Figs. 4.12 and 4.13).

figure r
figure s
Fig. 4.12
figure 12

Each of the bivariate plots in a pairs plot collage may be zoomed up and explored further

Fig. 4.13
figure 13

A more elaborate 6D pairs plot showing the type and scale of each variable and their bivariate relations

Figure 4.12 represents just one of the plots shown in the collage on Fig. 4.13. We can see that Focus and PostTraum have no relationship - Focus can equal to 3 or 1 in either of the PostTraum values (0 or 1). On the other hand, larger supp tends to correspond to larger qual values.

To see this trend we can also make a plot using qplot function. This allow us to add a smooth model curve forecasting a possible trend (Fig. 4.14).

figure t
Fig. 4.14
figure 14

Plotting the bivariate trend along with its confidence limits

You can also use the human height and weight dataset or the knee pain dataset to illustrate some interesting scatter plots.

4.4.2 Jitter Plot

Jitter plots can help us deal with the complexity issues when we have many points in the data. The function we will be using is in package ggplot2 is called position_jitter ().

Let’s use the earthquake data for this example. We will compare the differences with and without the position_jitter () function (Figs. 4.15 and 4.16) .

figure u
Fig. 4.15
figure 15

Jitter plot of magnitude type against depth and latitude (Earthquake dataset)

figure v
Fig. 4.16
figure 16

A lower opacity jitter plot of magnitude type against depth and latitude

Note that with option alpha=0.5 the “crowded” places are darker than the places with only one data point. Sometimes, we need to add text to these points, i.e., add label in aes or add geom_text. The result may look messy (Fig. 4.17).

figure w
Fig. 4.17
figure 17

Another version of the jitter plot of magnitude type explicitly listing the Earthquake ID label

Let’s try to fix the overlap of points and labels. We need to add check_overlap in geom_text and adjust the positions of the text labels with respect to the points (Figs. 4.18 and 4.19).

Fig. 4.18
figure 18

Yet another version of the previous jitter plot illustrating label specifications

Fig. 4.19
figure 19

This jitter plot suppresses the scatter point bubbles in favor of ID labels

figure x
figure y

4.4.3 Bar Plots

Bar plots, or bar charts, represent group data with rectangular bars. There are many variants of bar charts for comparison among categories. Typically, either horizontal or vertical bars are used where one of the axes shows the compared categories and the other axis represents a discrete value. It’s possible, and sometimes desirable, to plot bar graphs including bars clustered by groups.

In R, we have the barplot () function to generate bar plots. The input for barplot() is either a vector or a matrix (Fig. 4.20) .

figure z
Fig. 4.20
figure 20

Example of a labeled boxplot using simulated data with grouping categorical labels

It may require some creativity to add value labels on each bar. First, let’s specify the location on the x-axis x=seq(1.5, 21, by=1)+ rep(c(0, 1, 2, 3, 4), each=4). In this example there are 20 bars. The x location for middle of the first bar is 1.5 (there is one empty space before the first bar). The middle of the last bar is 24.5. seq(1.5, 21, by=1) starts at 1.5 and creates 20 bars that end with x=21. Then, we use rep(c(0, 1, 2, 3, 4), each=4) to add 0 to the first group, 1 to the second group, and so forth. Thus, we have the desired positions on the x-axis. The y-axis positions are obtained just by adding 0.1 to each bar height.

We can also add standard deviations to the means on the bars. To do this, we need to use the arrows() function and the option angle = 90, the result is shown on Fig. 4.21 .

figure aa
Fig. 4.21
figure 21

Statistical barplot showing point-estimates and their error limits (simulated data)

Let’s look at a more complex example. We will utilize the dataset Case_04_ChildTrauma for illustration. This case study examines associations between post-traumatic psychopathology and service utilization by trauma-exposed children.

figure ab

We have two character variables. Our goal is to draw a bar plot comparing the means of age and service among different races in this study, and we want to add standard deviation to each bar. The first thing to do is to delete the two character columns. Remember, the input for barplot () is a numerical vector or a matrix. However, we will need race information for the categorical classification. Thus, we will store race in a different variable.

figure ac

We are now ready to separate the groups and compute the group means.

figure ad

Now that we have a numerical matrix for the means, we can compute a second order statistics, standard deviation, and plot it along with the means, to illustrate the amount of dispersion for each variable (Fig. 4.22).

figure ae
Fig. 4.22
figure 22

Barplot showing point-estimates and their error limits (Child Trauma dataset)

Here, we want the y margin to be a little higher than the greatest value (ylim=c(0, max(x)+2.0)) because we need to leave space for value labels. The plot shows that Hispanic trauma-exposed children may be younger, in terms of average age, and less likely to utilize services like primary care, emergency room, outpatient therapy, outpatient psychiatrist, etc.

Another way to plot bar plots is to use ggplot() in the ggplot package. This kind of bar plot is quite different from the one we introduced previously. It displays the counts of character variables rather than the means of numerical variables. It takes the values from a data.frame . Unlike barplot (), drawing bar plots using ggplot2 requires that the character variables remain in the original data frame (Fig. 4.23).

figure af
Fig. 4.23
figure 23

Barplot of counts for different types of child trauma by race (color label)

This plot helps us compare the occurrence of different types of child-trauma among different races.

4.4.4 Trees and Graphs

In general, a graph is an ordered pair G = (V, E) of vertices (V), i.e., nodes or points, and edges (E), arcs or lines connecting pairs of nodes in V. A tree is a special type of acyclic graph that does not include looping paths. Visualization of graphs is critical in many biosocial and health studies, and we will see menu such examples throughout this textbook.

In Chaps. 10 and 13, we will learn more about how to build tree models and other clustering methods, and in Chap. 23 , we will discuss deep learning and neural networks, which have a direct graphical representation.

This section will be focused on displaying tree graphs. We will use a self-efficacy study, 02_Nof1_Data.csv, for this demonstration.

figure ag

We will use hclust to build the hierarchical cluster model. hclust takes only inputs that have dissimilarity structure as produced by dist(). Also, we use the ave method for agglomeration, see the tree graph on Fig. 4.24 .

figure ah
Fig. 4.24
figure 24

Hierarchical clustering dendrogram of the 900 self-efficacy records of 30 participants including the nine features tracked over a month

When we specify no limit for the maximum cluster groups, we will get the graph, on Fig. 4.24, which is not easy to interpret. Luckily, cutree will help us limit the cluster numbers. cutree() takes a hclust object and returns a vector of group indicators for all observations.

figure ai

Usinf a for loop, we can get the mean of each variable within groups.

figure aj

Now, we can plot a new tree graph with ten groups. Using the members= table (mem) option, the matrix is taken to be a dissimilarity matrix between clusters, instead of dissimilarities between singletons, and members represents the number of observations per cluster (Fig. 4.25).

figure ak
Fig. 4.25
figure 25

A ten-cluster hierarchical dendrogram of the same dataset as before

4.4.5 Correlation Plots

The corrplot package enables the graphical display of correlation matrices, confidence intervals, and other plots showing matrix reordering. There are seven visualization methods (parameter methods) in corrplot package, named "circle", "square", "ellipse", "number", "shade", "color", and "pie".

Let’s use 03_NC _SNP_ROI_Assoc_P_values.csv again to investigate the associations among SNPs using correlation plots.

The corrplot() function we will be using only accepts correlation matrices. So, we need to first obtain the correlation matrix of our data first using the cor() function.

figure al

We will illustrate alternative correlation plots using the corrplot function in Figs. 4.26, 4.27, 4.28, 4.29, 4.30, and 4.31.

figure am
Fig. 4.26
figure 26

Correlation plot of regional brain volumes of the healthy normal controls using circles

figure an
Fig. 4.27
figure 27

The same correlation plot of regional NC brain volumes using squares

figure ao
Fig. 4.28
figure 28

The same correlation plot of regional NC brain volumes using ellipses

figure ap
Fig. 4.29
figure 29

The same correlation plot of regional NC brain volumes using pie segments

figure aq
Fig. 4.30
figure 30

Upper diagonal correlation plot of regional NC brain volumes using circles

figure ar
Fig. 4.31
figure 31

Mixed correlation plot of regional NC brain volumes using circles and numbers

In the figures above, different shades of colors represent low-and-high correlations of the two variables corresponding to the x and y indices .

4.5 Relationships

4.5.1 Line Plots Using ggplot

Line charts display a series of data points (e.g., observed intensities (Y) over time (X)) by connecting them with straight-line segments. These can be used to either track temporal changes of a process or compare the trajectories of multiple cases, time series, or subjects over time, space, or state.

In this section, we will utilize the Earthquakes dataset on SOCR website. It records information about earthquakes that occurred between 1969 and 2007 with magnitudes larger than 5 on the Richter scale.

figure as

In this dataset, we group the data by Magt (magnitude type). We will draw a “Depth vs. Latitude” line plot from this dataset. The function we are using is called ggplot() under ggplot2 . The input type for this function is a data frame and aes() specifies aesthetic mappings of how variables in the data are mapped to visual properties (aesthetics) of the geom objects, e.g., lines (Fig. 4.32).

figure at
Fig. 4.32
figure 32

Line plot of Earthquake magnitude type by its ground depth and latitude

There are two important components in the script. The first part, ggplot(earthquake, aes(Depth, Latitude, group=Magt, color =Magt)), specifies the setting of the plot: dataset, group, and color. The second part specifies that we are going to draw lines between data points. In later chapters, we will frequently use package ggplot2 whose generic structure always involves concatenating function calls like function1+function2+….

4.5.2 Density Plots

We can visualize the distribution of different variables using density plots.

The following segment of R code plots the distribution for latitude among different earthquake magnitude types. Also, it uses the ggplot() function combined with geom_density () (Fig. 4.33).

figure au
Fig. 4.33
figure 33

Density plot of Earthquakes according to their magnitude types and latitude location

figure av

Note the green magt type (Local (ML) earthquakes) peaks at latitude 37.5, which represents 37–38° North, near San Francisco, California.

4.5.3 Distributions

Probability distribution plots depict the characteristics of the underlying process that can be used to contrast and compare the shapes of distributions as proxy of the corresponding natural phenomena. For univariate, bivariate, and multivariate processes, the distribution plots are drawn as curves, surfaces, or manifolds, respectively. These plots may be used to inspect areas under the distribution plot that correspond to either probabilities or data values. The Distributome Cauchy distribution calculator and the SOCR 2D bivariate Normal Distribution plot provide simple examples of distribution plots in 1D and 2D, respectively (Fig. 4.34).

Fig. 4.34
figure 34

Univariate and bivariate probability distribution calculators (Distributome Project)

4.5.4 2D Kernel Density and 3D Surface Plots

Density estimation is the process of using observed data to compute an estimate of the underlying process’ probability density function. There are several approaches to obtain density estimation, but the most basic technique is to use a rescaled histogram.

Plotting 2D Kernel Density and 3D Surface plots is very important and useful in multivariate exploratory data analytics.

We will use plot _ly() function under plotly package, which requires a data frame input.

To create a surface plot, we use two vectors: x and y with length m and n respectively. We also need a matrix: z of size m × n. This z matrix is created from matrix multiplication between x and y.

To plot the 2D Kernel Density estimation plot we will use the eruptions data from the “Old Faithful” geyser in Yellowstone National Park, Wyoming, stored in R as geyser. Also, the kde2d() function is needed for 2D kernel density estimation.

figure aw

Here z=t(x)%*%y and we apply plot _ly to the list kd via the with () function (Fig. 4.35) .

figure ax
Fig. 4.35
figure 35

Interactive surface plot of kernel density for the Old Faithful geyser eruptions

Note that we used the option "surface".

For 3D surfaces, we have a built-in dataset in R called volcano . It records the volcano height at location x, y (longitude, latitude). Because z is always made from x and y, we can simply specify z to get the complete surface plot (Fig. 4.36) .

figure ay
Fig. 4.36
figure 36

Interactive surface plot of kernel density for the R volcano dataset

4.5.5 Multiple 2D Image Surface Plots

Let’s look at another example using a 2D brain image (Fig. 4.37) .

figure az
Fig. 4.37
figure 37

Interactive surface plot of kernel density for the 2D brain imaging data

The DSPA Online appendix provides additional details on shape representation, modeling, and computing on surfaces and manifolds.

4.5.6 3D and 4D Visualizations

Many datasets have intrinsic multi-dimensional characteristics. For instance, the human body is a 3D solid of matter (three spatial dimensions can be used to describe the position of every component, e.g., sMRI volume) that changes over time (the fourth dimension, e.g., fMRI hypervolumes).

The SOCR BrainViewer shows how to use a web-browser to visualize 2D cross-sections of 3D volumes, display volume-rendering, and show 1D (e.g., 1-manifold curves embedded in 3D) and 2D (e.g., surfaces, shapes) models jointly into the same 3D scene (Fig. 4.38).

Fig. 4.38
figure 38

Live demo: interactive brain viewer

We will now illustrate an example of 3D/4D visualization in R using the packages brainR and rgl.

figure ba
figure bb

For 4D fMRI time-series, we can load the hypervolumes similarly and then display them (Figs. 4.39, 4.40, 4.41, 4.42, 4.43, and 4.44):

figure bc
Fig. 4.39
figure 39

2D cross-sectional (axial) views of the 4D fMRI data

figure bd
Fig. 4.40
figure 40

Histogram plot of the fMRI intensities

figure be
Fig. 4.41
figure 41

Coronal (top-left), sagittal (to-right), and axial (bottom-left) views of the 4D fMRI data

figure bf
Fig. 4.42
figure 42

Truncated histogram of the fMRI hyper-volume intensities

figure bg
Fig. 4.43
figure 43

Intensities of the fifth timepoint epoch of the 4D fMRI time series

figure bh
Fig. 4.44
figure 44

The complete time course of the raw (blue) and two smoothed versions of the fMRI timeseries at one specific voxel location (30, 30, 15)

Chapter 19 provides more details about longitudinal and time-series data analysis.

4.6 Appendix

4.6.1 Hands-on Activity (Health Behavior Risks)

figure bi

Classify the cases using these variables: "AGE_G" "SEX" "RACEGR3" "IMPEDUC" "IMPMRTL" "EMPLOY1" "INCOMG" "CVDINFR4" "CVDCRHD4" "CVDSTRK3" "DIABETE3" "RFSMOK3" "FRTLT1" "VEGLT1"

figure bj

Plot a clustering diagram (Fig. 4.45)

figure bk
Fig. 4.45
figure 45

Clustering dendrogram using the Health Behavior Risks case-study

figure bl

Let’s try to identify the number of cases for varying number of clusters.

figure bm
figure bn

Next, inspect the cluster labels for all SubjectIDs:

figure bo

We can examine which TOTINDA (Leisure time physical activities per month, 1 = Yes, 2 = No, 9 = Don’t know/Refused/Missing) and which RFDRHV4 are in which clusters (Fig. 4.46):

figure bp
figure bq
Fig. 4.46
figure 46

Scatterplot between two variables in the Health Behavior Risks case-study

figure br

To characterize the clusters, we can look at cluster summary statistics, like the median, of the variables that were used to perform the cluster analysis. These can be broken down by the groups identified by the cluster analysis. The aggregate function will compute statistics (e.g., median) on many variables simultaneously. Let’s examine the median values for each variable we used in the cluster analysis, broken up by cluster groups:

figure bs

4.6.2 Additional ggplot Examples

Below, we will show additional visualization examples.

4.6.2.1 Housing Price Data

This example uses the SOCR Home Price Index data of 19 major US cities from 1991 to 2009 (Fig. 4.47).

figure bt
Fig. 4.47
figure 47

Home price index plot over time

4.6.2.2 Modeling the Home Price Index Data (Fig. 4.48)

figure bu
Fig. 4.48
figure 48

Predicting the San Francisco home process using data from the Los Angeles home sales

We can also use ggplot to draw pairs plots (Fig. 4.49).

figure bv
Fig. 4.49
figure 49

A more elaborate pairs plot of the home price index dataset illustrating the distributions of home prices within a metropolitan area, as well as the paired relations between regions

4.6.2.3 Map of the Neighborhoods of Los Angeles (LA)

This example interrogates data of 110 LA neighborhoods, which includes measures of education, income, and population demographics.

Here, we select the Longitude and Latitude as the axes, mark these 110 Neighborhoods according to their population, fill out those points according to the income of each area, and label each neighborhood (Fig. 4.50).

figure bw
figure bx
Fig. 4.50
figure 50

Bubble plot of Los Angeles neighborhood location (longitude vs latitude), population size, and income

Observe that some areas (e.g., Beverly Hills) have disproportionately higher incomes. In addition, it is worth pointing out that the resulting plot resembles this plot of LA County (Fig. 4.51).

Fig. 4.51
figure 51

The Los Angeles county map resembles the plot on Fig. 4.50

4.6.2.4 Latin Letter Frequency in Different Languages

This example uses ggplot to interrogate the SOCR Latin letter frequency data, which includes the frequencies of the 26 common Latin characters in several derivative languages. There is quite a variation between the frequencies of Latin letters in different languages (Figs. 4.52 and 4.53).

figure by
Fig. 4.52
figure 52

Frequency distributions of Latin letters in several languages

figure bz
figure ca
Fig. 4.53
figure 53

Pie chart similar to the stacked bar chart, Fig. 4.52

You can experiment with the SOCR interactive motion chart, see Fig. 4.54.

Fig. 4.54
figure 54

Live demo: 6D SOCR MotionChart

4.7 Assignments 4: Data Visualization

4.7.1 Common Plots

Use the Divorce data (Case Study 01) or the TBI dataset (CaseStudy11_TBI) to generate appropriate visualization of histograms , density plots, pie charts, heatmaps, barplots, and paired correlation plots.

4.7.2 Trees and Graphs

Use the SOCR Resource Hierarchical data (JSON) or the DSPA Dynamic Certificate Map (JSON) to generate some tree/graph displays of the structural information.

The code fragment below shows an example of processing a JSON hierarchy.

figure cb

4.7.3 Exploratory Data Analytics (EDA)

  • Use SOCR Oil Gas Data to generate plots: (i) read data table, you may need to fill the inconsistent values with NAs; (ii) data preprocessing: select variables, type convert, etc.; (iii) generate two plots: the first plots includes two subplots, consumption plots and production plots; the second figure includes three subplots, for fossil, nuclear and renewable, respectively. To draw the subplots, you can use facet_grid(); (iv) all figures should have year as x axis; (v) the first figure should include three curves (fossil, nuclear and renewable) for each subplot; the second figure should include two curves (consumption and production) for each subplot.

  • Use SOCR Ozone Data to generate a correlation plot with the variables MTH_1, MTH_2, …, MTH_12. (Hint: you need to obtain the correlation matrix first, then apply the corrplot package. Try some alternative methods as well, circle, pie, mixed etc.)

  • Use SOCR CA Ozone Data to generate a 3D surface plot (Using variables Longitude, Latitude and O3).

  • Generate a sequence of random numbers from student t distribution. Draw the sample histogram and compare it with normal distribution. Try different degrees of freedom. What do you find? Does varying the seed and regenerating the student t sample change that conclusion?

  • Use the SOCR Parkinson’s Big Meta data (only rows with time=0) to generate a heatmap plot. Set RowSideColors, ColSideColors and rainbow. (Hint: you may need to select columns, properly convert the data, and normalize it.)

  • Use SOCR 2011 US Jobs Ranking draw scatter plot Overall_Score vs. Average_Income(USD) include title and label the axes. Then try qplot for Overall_Score vs. Average_Income(USD): (1) fill with the Stress_Level; (2) size the points according to Hiring_Potential; and (3) label using Job_Title.

  • Use SOCR Turkiye Student Evaluation Data to generate trees and graphs, using cutree() and select any k you prefer. (Use variables Q1–Q28).