0% found this document useful (0 votes)
33 views23 pages

Lesson 6 - Data Analytics - Visualizing

The document provides an overview of data visualization techniques using R, including examples of charts such as line charts, histograms, and pie charts. It emphasizes the importance of visual representation in data analysis and includes code snippets for creating various types of visualizations. Additionally, it references resources for further learning about data visualization with R.

Uploaded by

ngqtuananh2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views23 pages

Lesson 6 - Data Analytics - Visualizing

The document provides an overview of data visualization techniques using R, including examples of charts such as line charts, histograms, and pie charts. It emphasizes the importance of visual representation in data analysis and includes code snippets for creating various types of visualizations. Additionally, it references resources for further learning about data visualization with R.

Uploaded by

ngqtuananh2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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!

You might also like