Welcome to an immersive exploration of data visualization techniques tailored for in-depth community analytics. In this codethrough, we revisit and extend our learnings from Lab 03: Identifying Neighborhood Clusters, where we delved into clustering and examined the characteristics of different neighborhood groups. Now, armed with the prowess of R programming and Plotly, we embark on a journey to enhance our data visualization skills and gain fresh insights into the intricate details of community analytics.
Our codethrough will unfold in three compelling chapters:
Group Analysis Revisited We kick off by revisiting the comprehensive analysis of Group 1 conducted in Lab 03. An array of 30 variables is expertly visualized to gain a nuanced understanding of this specific neighborhood cluster.
Heatmaps: Unveiling Relationships Dive into the dynamic world of heatmaps, a potent tool for visualizing relationships within our dataset. The code will demonstrate how to construct a heatmap using Plotly, providing an intuitive overview of variable correlations within the community.
3D Scatter Plots: Exploring Dimensions Elevate your analysis with 3D scatter plots, a sophisticated technique for exploring relationships across three variables simultaneously. Using Plotly, we showcase the interplay between education, employment, and income, uncovering intricate patterns within our community data.
Data visualization is the cornerstone of effective data analysis and communication. As community analysts, mastering advanced visualization techniques equips us to uncover hidden patterns, communicate complex insights, and make informed decisions. Whether you’re revisiting previous analyses or venturing into uncharted territory, the skills acquired in this codethrough will empower you to elevate your community analytics game, making your visualizations more impactful and your insights more profound. Get ready to bring your data to life!
By the conclusion of this codethrough, you will:
Revisit Group Analysis Techniques: Gain proficiency in revisiting and extending group analysis, as demonstrated through the comprehensive exploration of Group 1’s characteristics. Enhance your ability to visualize and interpret complex patterns within specific neighborhood clusters.
Master Heatmap Construction: Acquire the skills to construct informative and visually appealing heatmaps using Plotly. Understand the nuances of representing variable relationships through color-coded intensity, adding a layer of depth to your exploratory data analysis.
Explore 3D Scatter Plots: Develop expertise in constructing and customizing 3D scatter plots using Plotly. Uncover insights into multi-variable relationships within community data, elevating your ability to discern intricate patterns in three-dimensional space.
Enhance Data Visualization Proficiency: Strengthen your overall data visualization proficiency, learning to leverage advanced tools and techniques for clearer communication of insights. Explore the versatility of Plotly as a powerful platform for creating interactive and impactful visualizations.
Apply Techniques to Your Own Analyses: Acquire the knowledge and skills necessary to adapt the presented techniques to your own community analytics projects. Whether revisiting previous analyses or exploring new datasets, you’ll be equipped to create engaging visualizations that enhance the depth and clarity of your insights.
To begin, you need to install the plotly package. You can do this by running the following command in your R console:
Once the package is installed, you can load it into your R session using the following command:
In this code-through, we will focus on visualizing data used in Lab-03: Identifying Neighborhood Clusters. The primary goal is to enhance our understanding of the steps involved in preparing and visualizing data for clustering analysis. This process is crucial for gaining insights into neighborhood patterns and identifying potential clusters based on various features.
data.dictionary <-
structure(list(LABEL = c("tractid", "pnhwht12", "pnhblk12", "phisp12",
"pntv12", "pfb12", "polang12", "phs12", "pcol12", "punemp12",
"pflabf12", "pprof12", "pmanuf12", "pvet12", "psemp12", "hinc12",
"incpc12", "ppov12", "pown12", "pvac12", "pmulti12", "mrent12",
"mhmval12", "p30old12", "p10yrs12", "p18und12", "p60up12", "p75up12",
"pmar12", "pwds12", "pfhh12"), VARIABLE = c("GEOID", "Percent white, non-Hispanic",
"Percent black, non-Hispanic", "Percent Hispanic", "Percent Native American race",
"Percent foreign born", "Percent speaking other language at home, age 5 plus",
"Percent with high school degree or less", "Percent with 4-year college degree or more",
"Percent unemployed", "Percent female labor force participation",
"Percent professional employees", "Percent manufacturing employees",
"Percent veteran", "Percent self-employed", "Median HH income, total",
"Per capita income", "Percent in poverty, total", "Percent owner-occupied units",
"Percent vacant units", "Percent multi-family units", "Median rent",
"Median home value", "Percent structures more than 30 years old",
"Percent HH in neighborhood 10 years or less", "Percent 17 and under, total",
"Percent 60 and older, total", "Percent 75 and older, total",
"Percent currently married, not separated", "Percent widowed, divorced and separated",
"Percent female-headed families with children")), class = "data.frame", row.names = c(NA,
-31L))github.url <- "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/phx_dorling.geojson"
phx <- geojson_read( x=github.url, what="sp" )phx <- spTransform( phx, CRS("+init=epsg:3395") )
bb <- st_bbox( c( xmin = -12519146, xmax = -12421368,
ymax = 3965924, ymin = 3899074 ),
crs = st_crs("+init=epsg:3395"))keep.these <- c("pnhwht12", "pnhblk12", "phisp12", "pntv12", "pfb12", "polang12",
"phs12", "pcol12", "punemp12", "pflabf12", "pprof12", "pmanuf12",
"pvet12", "psemp12", "hinc12", "incpc12", "ppov12", "pown12",
"pvac12", "pmulti12", "mrent12", "mhmval12", "p30old12", "p10yrs12",
"p18und12", "p60up12", "p75up12", "pmar12", "pwds12", "pfhh12")
d2 <- select( d1, keep.these )
d3 <- apply( d2, 2, scale )As a recap from Lab-03, our analysis involved the creation of eight distinct clusters. In this code-through, we will explore the utilization of this clustered data to generate visually compelling plots. This step-by-step walkthrough aims to enhance your understanding of cluster analysis and its application to produce insightful visualizations.
phx2 <- phx[ !(phx$GEOID %in% c("04013723308","04013723304")) , ]
tmap_mode("plot")
tmap_style("cobalt")
tm_shape( phx2, bbox=bb ) +
tm_polygons( col="cluster", palette="Accent" )In Lab-03, our focus was on examining the variations in neighborhood clusters. Today we are concerned with the percentage of residents with a college degree. This analysis provided valuable insights into the educational landscape across different clusters.
d2$cluster <- as.factor( paste0("GROUP-",fit$classification) )
ggplot( d2, aes( x=pcol12) ) +
geom_density( alpha = 0.5, fill="blue" ) + # xlim( -3, 3 ) +
xlab( "Percent with a College Degree" ) + facet_wrap( ~ cluster, nrow=2 ) + theme_minimal()In this code-through, we will elevate our data visualization techniques by transitioning from the conventional density graphs used in Lab-03 to vibrant and informative violin graphs. Violin graphs not only depict the density of data points but also introduce color to convey additional insights. The use of colors enhances the visual representation, allowing for a more nuanced understanding of the distribution of variables within and across clusters. This exploration aims to demonstrate the effectiveness of colorful violin graphs as a powerful tool for conveying complex information in a visually appealing manner.
# Step 1: Load necessary libraries (if not done previously)
library(plotly)
# Step 2: Create Colorful Violin Plots for Educational Attainment Across Clusters
fig <- d2 %>%
plot_ly(
x = ~cluster,
y = ~pcol12,
split = ~cluster,
type = 'violin',
box = list(
visible = T
),
meanline = list(
visible = T
),
colors = "Accent" # Assigning a color palette for visual distinction
)
# Step 3: Customize Layout
fig <- fig %>%
layout(
xaxis = list(
title = "Cluster"
),
yaxis = list(
title = "Percent with a College Degree",
zeroline = F
)
)
# Step 4: Display the Violin Plot
figIn this section, we revisit the analysis of Group 1 conducted in Lab 03. The graph presented effectively represents the 30 variables of interest, providing insights into the characteristics of this specific group.
df.pct <- sapply( d2, ntile, 100 )
d3 <- as.data.frame( df.pct )
d3$cluster <- as.factor( paste0("GROUP-",fit$classification) )
stats <-
d3 %>%
group_by( cluster ) %>%
summarise_each( funs(mean) )
t <- data.frame( t(stats), stringsAsFactors=F )
names(t) <- paste0( "GROUP.", 1:8 )
t <- t[-1,]
z <- t[,1]
plot( rep(1,30), 1:30, bty="n", xlim=c(-75,100),
type="n", xaxt="n", yaxt="n",
xlab="Percentile", ylab="",
main=paste("GROUP 1") )
abline( v=seq(0,100,25), lty=3, lwd=1.5, col="gray90" )
segments( y0=1:30, x0=0, x1=100, col="gray70", lwd=2 )
text( -0.2, 1:30, data.dictionary$VARIABLE[-1], cex=0.85, pos=2 )
points( z, 1:30, pch=19, col="firebrick", cex=1.5 )
axis( side=1, at=c(0,50,100), col.axis="gray", col="gray" )As an alternative visualization approach, we explore the use of Plotly to create clean and dynamic histogram visualizations. This option allows for interactive exploration and a visually engaging presentation of the data, offering a different perspective on the distribution of variables within Group 1.
The following steps will guide us through both the traditional graph and the utilization of Plotly for histogram visualizations, providing flexibility and enhanced visualization capabilities for analyzing Group 1.
# Creating Horizontal Bar Chart for GROUP 1
# Step 1: Load necessary libraries (if not previously loaded.)
library(plotly)
# Step 2: Define Data and Chart
fig <- plot_ly(
x = t$GROUP.1,
y = data.dictionary$VARIABLE[-1],
type = 'bar',
orientation = 'h',
xaxis = list(
title = 'Percentile',
zerolinewidth = 20
)
)
# Step 3: Customize Layout
fig <- fig %>%
layout(
xaxis = list(title = "Percentile"),
title = "GROUP 1"
)
# Step 4: Display the Horizontal Bar Chart
figIn this segment, we delve into the use of heatmaps to uncover relationships within our dataset. Heatmaps are valuable tools for visualizing the intensity of a phenomenon across two dimensions, providing a clear and intuitive representation of data patterns.
The presented code employs Plotly to construct a heatmap that visualizes the relationship between variables such as the percentage of individuals with a 4-year college degree or more, the percentage of professional employees, and median household income. Each cell in the heatmap is color-coded based on the intensity of the variable, offering a quick and insightful overview of potential correlations.
The customization of axis titles ensures clarity in interpreting the heatmap, contributing to a comprehensive exploration of inter-variable relationships. This code serves as a versatile template for similar analyses, allowing users to adapt and apply it to their specific datasets and research questions
# Creating a Heatmap
# Step 1: Load necessary libraries (if not done previously.)
library(plotly)
# Step 2: Define Data and Create Heatmap
heatmap <- plot_ly(
data = d3,
type = "heatmap",
z = ~hinc12,
x = ~pcol12,
y = ~pprof12,
colorscale = "Viridis"
)
# Step 3: Customize Layout
heatmap <- heatmap %>% layout(
xaxis = list(title = "Percent with 4-year college degree or more"),
yaxis = list(title = "Percent professional employees"),
zaxis = list(title = "Median HH income, total")
)
# Step 4: Display the Heatmap
heatmapIn this section, we venture into the dynamic realm of 3D scatter plots, a powerful visualization technique that allows us to explore relationships across three variables simultaneously. The provided code utilizes Plotly to create an interactive 3D scatter plot, showcasing the interplay between the percentage of individuals with a 4-year college degree or more, the percentage of professional employees, and median household income.
Each data point is represented as a marker in the three-dimensional space, with marker size and color encoding additional information about the median household income. This approach not only adds a visual dimension to the analysis but also enables a nuanced understanding of variable relationships.
Customizing the layout with axis titles enhances the interpretability of the 3D scatter plot, providing researchers with a tool to gain deeper insights into complex datasets. This code can be adapted and extended for diverse datasets, offering a versatile framework for exploring multi-variable relationships in a visually compelling manner.
# Creating 3D Scatter Plot
# Step 1: Load necessary libraries (If not previously loaded.)
library(plotly)
# Step 2: Define Data and Create 3D Scatter Plot
scatter3d <- plot_ly(
data = d3,
type = "scatter3d",
mode = "markers",
x = ~pcol12,
y = ~pprof12,
z = ~hinc12,
marker = list(
size = 5,
color = ~hinc12, # Color based on the z-axis variable
colorscale = "Viridis"
)
)
# Step 3: Customize Layout
scatter3d <- scatter3d %>% layout(
scene = list(
xaxis = list(title = "Percent with 4-year college degree or more"),
yaxis = list(title = "Percent professional employees"),
zaxis = list(title = "Median HH income, total")
)
)
# Step 4: Display the 3D Scatter Plot
scatter3d
Learn more about Plotly with the following:
This code through references and cites the following sources: