Skip to Main Content

Data Visualization with R

Advanced Visualization Techniques

Complex Visualizations

While basic visualizations like bar charts and scatter plots are excellent for straightforward data representation, complex visualizations offer a more nuanced and in-depth look at your data. These advanced techniques allow you to explore multiple dimensions, relationships, and temporal changes in your dataset. They can include facets, interactive elements, and even animations to make your data come alive. In this section, we will explore some of these complex visualization methods using the mtcars dataset, aiming to provide you with the tools to create more insightful and interactive data stories

Faceted Bar Chart

Regular Faceted Bar Chart
library(ggplot2) 
ggplot(mtcars, aes(x=factor(cyl))) + 
   geom_bar() + 
   facet_wrap(~gear) + 
   xlab("Number of Cylinders") + 
   ylab("Frequency")
Customized Faceted Bar Chart
ggplot(mtcars, aes(x=factor(cyl))) + 
 geom_bar(fill="blue", color="black") + 
 facet_wrap(~gear) + 
 xlab("Number of Cylinders") + 
 ylab("Frequency") + 
 theme_minimal()

Interactive Scatter Plot with Plotly

Regular Interactive Scatter Plot
library(plotly) 
fig <- plot_ly(mtcars, x = ~hp, y = ~mpg, type = 'scatter', mode = 'markers') 
fig
Customized Interactive Scatter Plot
fig <- plot_ly(mtcars, x = ~hp, y = ~mpg, type = 'scatter', mode = 'markers') %>% 
 layout(title = "Customized Interactive Scatter Plot") 
fig

Heatmap

Regular Heatmap
library(heatmaply) 
heatmaply(cor(mtcars), 
              main = "Correlation Heatmap", 
              xlab = "Features", 
              ylab = "Features")
Customized Heatmap
heatmaply(cor(mtcars), 
              main = "Customized Correlation Heatmap", 
              xlab = "Features", 
              ylab = "Features", 
              colors = heat.colors(256))

3D Scatter Plot

Regular 3D Scatter Plot
library(scatterplot3d) 
scatterplot3d(mtcars$hp, mtcars$mpg, mtcars$wt, 
              main="3D Scatter Plot")
Customized 3D Scatter Plot
scatterplot3d(mtcars$hp, mtcars$mpg, mtcars$wt, 
               main="Customized 3D Scatter Plot", 
               color="blue", 
               pch=19, 
               type="h")

These examples showcase how you can create more complex visualizations using the mtcars dataset. These complex charts can provide deeper insights and are particularly useful when you have multi-dimensional data or when you want to create more interactive visual experiences.