Unit-5

 , Styling plots, Box plots, Heatmaps, Scatter plots with histograms, A scatter plot matrix, Area plots, Bubble charts, Hexagon bin plots, Trellis plots, 3D plot of a surface. Plotting and Visualization: A brief matplotlib API primer, plotting functions in pandas, Plotting Maps, Python Visualization Tool Ecosystem.

1. Playing with text

High-quality visualization is predicated on clean data and meticulous control over aesthetic details. The concept of "Playing with Text" encompasses two vital stages: pre-processing messy text data for structural integrity and using text elements for informative plot annotation. If raw categorical data is inconsistent (e.g., slightly varied spellings of the same category), the visualization will fail conceptually.

a. Text Data Pre-processing

In R, managing text data is streamlined by the stringr package. stringr provides cohesive functions for advanced techniques like extracting targeted substrings, globally replacing patterns and splitting text fields. This manipulation is often necessary to standardize labels or derive new categorical variables from unstructured text, ultimately cleaning the data before it reaches the visualization engine.

b. Visual Text Annotation

Once data is clean, visualization tools provide methods for adding context. In R, ggplot2 utilizes geom_text() or annotate () for placing labels, ensuring text aligns correctly with the data coordinate system.

In R, you can use the text() function to add custom text on a plot.

Example: 1

x <- 1:10

y <- x^2

plot(x, y, type = "b", col = "blue", pch = 19)

title(main = "Square Function", xlab = "X", ylab = "Y = X^2")

text(5, 60, "Mid Point", col = "red", cex = 1.2, pos = 3)

Example: 2

# Basic plot with text annotation

x <- c(1, 2, 3, 4)

y <- c(10, 20, 25, 30)

 plot(x, y, type = "o", main = "Plot with Text", xlab = "X-axis", ylab = "Y-axis")

text(3, 25, "Peak Point", col = "red", cex = 1.2)  # Add text annotation at specific coordinates

Example3

# Highlight a specific data point with text annotation
peak_car <- mpg[which.max(mpg$hwy), ]

ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point(aes(color = class)) +
  # Add the text label directly using geom_text
  geom_text(data = peak_car,
            aes(label = model),
            vjust = -1, hjust = 1, size = 3, color = "black") +
  # Add a static descriptive annotation using annotate()
  annotate("text", x = 6, y = 30,
           label = "Note: Lower displacement generally yields higher mileage.",
           size = 3.5, color = "darkgreen") +
  labs(title = "Highest MPG Vehicle Highlighted")


2. Styling Plots:

In R, aesthetic control is split. Non-data components (such as axis titles, backgrounds and grid lines) are managed by the comprehensive theme() function. Data-linked aesthetics are controlled via specific scale functions like scale_linetype_manual(), which allows the user to explicitly define the visual values (e.g., values=c("twodash", "dotted")) assigned to each data level.

Moreover, Styling plots allows for more visually appealing graphics, making it easier to communicate data effectively. You can apply custom styles in R using base functions or packages like basics: par(), at advanced level: ggplot2().

Example 1: 

par(bg = "lightgray", col.lab = "blue", col.main = "darkred")

plot(cars, pch = 19, col = "darkgreen", main = "Styled Cars Plot")




Example 2:
library(ggplot2)
ggplot(cars, aes(speed, dist)) +
  geom_point(color = "steelblue") +
  theme_minimal() +
  labs(title = "Car Speed vs Distance", x = "Speed", y = "Distance")




Mechanism/ConceptR (ggplot2/Grammar of Graphics)Python (Matplotlib OO/Imperative)Design Context
Mapping Data to Visualaes() function. Maps variable to scale.Keyword arguments within plot functions (e.g., c='red'). Direct attribute setting.R is highly declarative; Python is direct and imperative.
Bypassing ScalesI() function (AsIs) for literal values.Direct assignment to an Artist object's properties (e.g., line.set_color()).R provides an explicit escape route (I()); Matplotlib is inherently direct.
Bulk Styling Controltheme() for non-data elements; scale_..._manual() for data elements.setp() utility for simultaneous, multi-artist property adjustment.Matplotlib requires setp() for granular object-level consistency.

Different Statistical Visualization Plots

a) Scatter Plot Matrix

A scatter plot matrix shows the pairwise relationships between several variables, which is helpful for identifying correlations.

R Implementation: 

The generation of a truly integrated scatter plot matrix often relies on specialized packages like GGally (specifically the ggpairs() function), which automates the construction of the entire grid of layered ggplot components. When manually assembling such complex, multi-plot figures from independent ggplot objects, R requires powerful layout managers such as cowplot or patchwork. Functions like plot_grid() are necessary to combine the plots, specify the number of rows/columns, and, crucially, control the relative sizing of individual plots using parameters like rel_widths and rel_heights. The necessity of using external tools like cowplot to manage the figure’s overall grid structure reveals that R’s core strength lies at the individual plot (Axes) level, requiring specialized external libraries to match the Figure-level management intrinsic to Python’s OO architecture.


Python Implementation: 

The construction is handled efficiently by seaborn.pairplot(), which automatically manages the Matplotlib OO structure to build the full matrix of pairwise comparisons, typically with density estimates on the diagonal.

Example

pairs(~ mpg + hp + wt + disp, data = mtcars, col = "blue", pch = 19)


Example:

install.packages("psych")

library(psych)

pairs.panels(mtcars[, c("mpg", "hp", "wt", "disp")],

             method = "pearson",  # correlation method

             hist.col = "lightblue",

             density = TRUE,

             ellipses = TRUE)



b) Scatter Plots with Histograms (Marginal Distributions)

This composite visualization pairs a central scatter plot (analyzing bivariate relationships) with marginal histograms or density plots along the axes, offering crucial insight into the individual variable distributions, density concentrations and potential non-normal characteristics.

R Implementation (Layout Dependence): 

ggplot2 lacks a single integrated function for this plot. It requires the manual construction of three separate plots: the scatter plot (geom_point()) and two marginal histograms or density plots (geom_histogram() or geom_density()). Critically, these independent ggplot objects must then be precisely arranged using an external layout management package, such as cowplot or patchwork. These tools allow for meticulous control over the grid arrangement, shared axis removal, and ensuring the marginal plots align perfectly with the main scatter plot axes.


Python Implementation (Integrated Approach): This visualization is simplified significantly by statistical abstraction libraries. seaborn.jointplot() is the canonical method, which handles the complex Matplotlib Figure/Axes composition internally, automatically drawing the scatter plot and the aligned marginal distributions (histograms or kernel density estimates) with minimal user input.

Example

### Sactter plot with histogram
# 1. Create the central scatter plot
p_scatter <- ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  labs(x = "Displacement", y = "Highway MPG") +
  theme_classic()
# 2. Create the marginal histogram for X-axis (displ)
p_hist_x <- ggplot(mpg, aes(x = displ)) +
  geom_histogram(bins = 15, fill = "blue", alpha = 0.7) +
  theme_void() + # Minimal theme for marginal plot
  theme(axis.title.x = element_blank()) # Remove X-axis title
# 3. Create the marginal histogram for Y-axis (hwy)
p_hist_y <- ggplot(mpg, aes(x = hwy)) +
  geom_histogram(bins = 15, fill = "red", alpha = 0.7) +
  coord_flip() + # Flip coordinates to align vertically
  theme_void() +
  theme(axis.title.y = element_blank())
# 4. Combine and align using cowplot::plot_grid
plot_grid(p_hist_x, NULL, p_scatter, p_hist_y,
          ncol = 2, nrow = 2,
          rel_heights = c(1, 4),  # X-hist is 1 unit high, scatter is 4
          rel_widths = c(4, 1),   # Scatter is 4 units wide, Y-hist is 1
          align = 'hv'            # Align axes horizontally and vertically
)


c) Area Plot

Area plots are used to represent quantitative data, particularly to show cumulative totals or time-series evolution. Stacked area plots show changes in composition over time.

R Implementation: 

geom_area() in ggplot2. Proper handling of stacked data requires specifying the position argument (position="stack"). Effective use of the alpha aesthetic (transparency) is essential in stacked plots to prevent visual clutter and manage overplotting of segment boundaries.

Python Implementation: 

Achieved using Matplotlib’s low-level pyplot.fill_between() or, more commonly, through the integrated pandas.DataFrame.plot(kind='area') function, which provides a high-level wrapper suitable for rapid exploratory data analysis.

Example

x <- 1:10

y <- cumsum(rnorm(10))

plot(x, y, type = "n", main = "Area Plot")

polygon(c(x, rev(x)), c(y, rep(0, length(y))), col = "lightblue", border = NA)



ggplot(cse, aes(date, unemploy)) +
                                   geom_area(fill = "steelblue", alpha = 0.6)



d) Bubble Charts

Bubble charts are scatter plot extensions where a third quantitative variable is mapped to the size aesthetic of the point marker.

R Implementation: 

Generated by mapping a continuous variable to the size aesthetic within geom_point() in ggplot2. Careful attention must be paid to the scale of the size aesthetic to ensure the resulting visualization accurately reflects the magnitude of the third variable without distorting perception.

Python Implementation: 

Implemented using matplotlib.pyplot.scatter(), where the s (size) parameter accepts an array corresponding to the third quantitative variable.

Example

ggplot(mtcars, aes(x = wt, y = mpg, size = hp, color = cyl)) +

  geom_point(alpha = 0.6) +

  labs(title = "Bubble Chart: Weight vs MPG vs HP")




e) Heat Maps

Heatmaps are powerful for visualizing two-dimensional matrix data, where a third dimension (value or density) is represented by color intensity. They are commonly used for correlation matrices or aggregated 2D data distributions.


R Implementation: Basic heatmaps are constructed in ggplot2 using geom_tile() or geom_raster(). For applications like gene expression or correlation analysis, specialized external packages provide features for hierarchical clustering and annotation, extending the basic Tidyverse functionality. 

Python Implementation: Matplotlib provides pyplot.imshow() for visualizing raw numerical arrays. However, the preferred tool is seaborn.heatmap(), which is optimized for statistical data, offering streamlined parameters for annotating cell values and masking redundant portions (e.g., the upper triangle of a symmetrical correlation matrix).

Example
matrix_data <- matrix(rnorm(100), nrow = 10)
heatmap(matrix_data, col = heat.colors(20), main = "Heatmap Example")

Example 
library(reshape2)
m <- melt(matrix_data)
ggplot(m, aes(Var1, Var2, fill = value)) + geom_tile() + scale_fill_viridis_c()


f) Box Plot

Box plots are indispensable for visually comparing the five-number summary (median, quartiles and range) of a continuous variable across multiple categories, quickly highlighting skewness and identifying outliers.

R Implementation: 

In ggplot2, box plots are generated using geom_boxplot(). Customization involves controlling parameters for whisker length, outlier aesthetics and the use of notched boxplots to indicate potential differences in medians.

Python Implementation: 

While Matplotlib’s pyplot.boxplot() offers the core functionality, advanced practitioners typically use the higher-level seaborn.boxplot(). Seaborn simplifies the integration with Pandas Data Frames and automatically handles grouping and aesthetic assignments based on data structure.

Example

boxplot(mpg ~ cyl, data = mtcars, col = c("lightblue", "lightgreen", "pink"), main = "MPG by Cylinders", xlab = "Cylinders", ylab = "Miles per Gallon")


g). Trellis Plots / Faceting (Conditional Views)

Trellis plots or small multiples, involve repeating the same plot structure across subsets of the data defined by one or more categorical variables. This technique, known as faceting in ggplot2, is indispensable for exploring interaction effects and comparing distributions under various conditions.

R Implementation (Intrinsic to ggplot2): This technique is a core strength of the Grammar of Graphics. facet_wrap() is used for conditioning based on a single variable (allowing the panels to wrap optimally), while facet_grid() imposes a strict two-dimensional grid structure based on two variables (rows and columns). Because faceting is handled as a layer within ggplot2, the resulting plots naturally share axes and inherit scales consistently, simplifying the comparison of subsets.

Python Implementation: While Matplotlib can construct the grid manually, seaborn.FacetGrid is the conceptual equivalent, providing a high-level interface that automatically handles the data subsetting, axis sharing, and repetition of the plot across the grid, mirroring the declarative efficiency of R for this specific task.

R Code:

# Facet_grid: Plotting hwy vs cty, conditioning on 'drv' (rows) and 'fl' (columns)
p_grid <- ggplot(mpg, aes(x = cty, y = hwy)) +
  geom_point() +
  facet_grid(drv ~ fl) + # Strict grid layout: drv by row, fl by column
  labs(title = "Facet Grid: Mileage by Drive Type and Fuel Type")

# Display p_grid
p_grid




h) Hexagon Bin Plots (Addressing Over plotting)

When visualizing massive datasets in a scatter plot format, over plotting renders the resulting image uninterpretable. Hexagon bin plots solve this problem by dividing the 2D space into hexagonal cells and using color intensity to represent the count or density of observations within each cell.

R Implementation: 
Implemented using geom_hex() within ggplot2, which requires the auxiliary hexbin package. Fine-tuning the resolution of the plot is controlled via the bins parameter.

Python Implementation: 
Implemented using matplotlib.pyplot.hexbin(). Control over resolution is achieved via the gridsize parameter, and density mapping is controlled by the cmap (color map) parameter.

Example-
# Note: Requires the 'hexbin' package installed
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_hex(bins = 15) + # Set the number of bins to control resolution
  # Use a sequential color scale for density
  scale_fill_viridis_c() +
  labs(title = "Hexagon Bin Plot: Density of Displacement vs. MPG",
       fill = "Count in Bin") +
  theme_bw()



i) 3D Plot of a Surface (Spatial and Multivariate Data)

Three-dimensional surface plots are used to visualize functions of two variables () or to render complex spatial data such as elevation maps, requiring true 3D projection.

R Implementation: 
For static rendering, packages like rgl are used. For interactive, web-based displays—which are often preferred for 3D visualization—the plotly package is essential. The process typically involves calculating the function over a grid and converting the output into a matrix format suitable for surface rendering.

Python Implementation: 
Achieved using Matplotlib’s mplot3d toolkit. Specifically, the Axes3D.plot_surface() or Axes3D.plot_wireframe() functions are used. It is important to note that static Matplotlib 3D output often lacks the intuitive rotation and viewing controls provided by interactive, modern web-based alternatives.

R Code: 
# Run this command ONLY ONCE to install the plotly package:
install.packages("plotly")
# Example data: Create a synthetic surface (Z = f(X, Y))
x <- seq(-10, 10, by = 0.5)
y <- seq(-10, 10, by = 0.5)
z <- outer(x, y, function(a, b) {
  # Sinc function (sin(r)/r) where r is the distance from the origin
  r <- sqrt(a^2 + b^2)
  ifelse(r == 0, 1, sin(r) / r)
})

# 1. Start the plot object
plot_surface_obj <- plotly::plot_ly(x = x, y = y, z = z)

# 2. Add the surface trace (using function chaining)
plot_surface_obj <- plotly::add_surface(plot_surface_obj)

# 3. Apply the layout and title (using function chaining)
plot_surface_obj <- plotly::layout(plot_surface_obj,
                                   title = "Interactive 3D Sinc Function Surface")

# Display the plot
plot_surface_obj






Specialized Visualization Domains and Tool Ecosystem

The modern data science toolkit extends beyond the core plotting libraries, integrating visualization directly into data structures and leveraging domain-specific or interactive frameworks.

A. Plotting Functions in Pandas

Pandas provides a high-level, integrated interface to Matplotlib, enabling rapid exploratory data analysis (EDA) directly from DataFrame objects. This integration significantly reduces the cognitive load for exploratory tasks.

The methods provided, such as df.plot(kind='bar'), df.plot(kind='line'), or df.hist(), act as wrappers around underlying Matplotlib calls, allowing users to quickly visualize the structure and distribution of their data without needing to manage the Figure and Axes setup manually. Critically, these Pandas plotting calls return the underlying Matplotlib Axes object. This provides a crucial bridge: the user can perform rapid initial plotting using the Pandas interface, and then immediately transition to advanced Matplotlib OO (Object Oriented) methods—such as ax.set_title() or the setp() utility—for fine-grained styling and customization.

B. Plotting Maps and Geospatial Visualization

Geospatial data visualization necessitates specialized libraries to handle coordinate systems, projections and geometric data structures.

  • R Ecosystem: R leverages the sf (simple features) package for handling spatial vector data. Visualization is often done using tmap for thematic mapping, or by integrating ggplot2 with geometric objects, ensuring correct projection handling.

  • Python Ecosystem: Python relies on GeoPandas for managing spatial data structures. Visualization often utilizes Cartopy for generating static map projections within Matplotlib, or Folium for creating interactive, web-based marker and choropleth maps, which leverages JavaScript mapping libraries. This indicates that geospatial analysis requires venturing beyond the standard Matplotlib/Seaborn core.

C. Python Visualization Tool Ecosystem

The Python visualization ecosystem is dynamic, expanding rapidly beyond Matplotlib to address demands for interactivity and declarative syntax, reflecting a desire to match the usability and consistency of R’s Tidyverse.

1. Interactive Visualization

While Matplotlib is powerful, its static nature is a limitation for presenting complex data. Interactive tools like Plotly and Bokeh address this by generating web-based charts that allow users to pan, zoom and drill down into data. These libraries are particularly valuable for dashboarding and sharing results with non-technical audiences.

2. Declarative Visualization

The Altair library introduces the Grammar of Graphics concept (similar to ggplot2) into Python, building upon the Vega-Lite specification. Altair offers a high-level, declarative approach, which stands in stark contrast to Matplotlib’s imperative style. The emergence of tools like Altair demonstrates a maturation of the Python ecosystem toward greater standardization and programmatic consistency, simplifying the creation of complex charts by abstracting away low-level Matplotlib details.

3. Specialization

For advanced applications, such as volumetric rendering or highly customized 3D data analysis (beyond the capabilities of Matplotlib’s basic mplot3d toolkit), tools like Mayavi or specialized 3D rendering libraries are necessary, extending Python's capabilities in scientific visualization.

Comments

Popular posts from this blog

UNIT-1

Unit-2