VISUALIZING DATA
“The simple graph has brought more information
to the data analyst’s mind than any other device.”
- John Tukey
US PRESIDENTIAL
ELECTION
2024
Source: [Link]
From charts …
Adapted from multiple sources
From charts …
Source: [Link]
From charts … to data dashboard
Adapted from multiple sources
Preparing example data
# Load packages
library(highcharter)
library(ggplot2)
library(gapminder) # Sample data for some demo charts
# Prepare data
top10_countries <- gapminder %>%
filter(year == max(year))%>%
arrange(desc(pop)) %>%
head(10)
Source: [Link]
DATA ANALYTICS WITH R | IEC UTH
Table format
Option 1:
● library(DT) ( Note: Please remember to install package first 😉 )
● datatable(top10_countries)
Option 2:
● library(reactable)
● reactable(top10_countries)
DATA ANALYTICS WITH R | IEC UTH
Line chart
Source:[Link]
DATA ANALYTICS WITH R | IEC UTH
Line chart
ggplot(data = country_data, aes(x = xValue, y = yValue))
+ geom_line()
Example:
us_data <- gapminder %>%
filter(country == "United States")
us_linechart <- ggplot(us_data, aes(x= year, y= pop))
+ geom_line()
Source: [Link]
DATA ANALYTICS WITH R | IEC UTH
Histogram
Let’s look at export_sales data
ggplot(data = export_sales,
aes(x = TotalPrice))
+ geom_histogram(binwidth = 500)
+ scale_fill_brewer(palette = "Set1")
DATA ANALYTICS WITH R | IEC UTH
Considering colors
Source: Guide to becoming a data storyteller
DATA ANALYTICS WITH R | IEC UTH
Considering colors
Source: Guide to becoming a data storyteller
DATA ANALYTICS WITH R | IEC UTH
Further adjustments for your charts
“ColorBrewer”
Run this in R:
RColorBrewer::[Link]()
DATA ANALYTICS WITH R | IEC UTH
Example
Add a line for mean value, and change colors
ggplot(export_sales,
aes(x = Category, y = TotalPrice, fill = Category))
+ geom_boxplot(alpha = 0.7)
+ stat_summary(fun.y = mean, geom = "point", shape = 20, size = 3,
color="red", fill="red")
+ scale_fill_brewer(palette = "BrBG")
+ theme([Link] = "none")
Source:
[Link]
DATA ANALYTICS WITH R | IEC UTH
Example
What if I want this view?
🤓
→ Your research homework
Charts with Highcharter
pie_chart <- top10_countries %>%
hchart("pie",
hcaes(x = country, y = pop))%>%
hc_title(text = "Top 10 countries by
population") %>%
hc_add_theme(hc_theme_ggplot2())
pie_chart
Source: [Link]
DATA ANALYTICS WITH R | IEC UTH
Charts with Highcharter
col_chart <- top10_countries %>%
hchart("column",
hcaes(x = country, y = pop),
color = "green")
col_chart
Now change the color code part with this:
color = "#004065"
Source: [Link]
DATA ANALYTICS WITH R | IEC UTH
Source: [Link]
Readings
● Visualize with R
● Ggplot quick start
● R graphics
● Highcharts demo
● Highcharts for R users
DATA ANALYTICS WITH R | IEC UTH
THANK YOU!