Analyzing and visualizing Data - Overview with 12 slides

profilesrinivas15
ITS530RAdvancedGraphsggplot.pptx

School of Computer & Information Sciences

ITS530 Analyzing and Visualizing Data

Introduction: R Advanced Graphs

ITS530 R Advanced Graphs

1

Revision Data Import & Entry

ITS530 R Advanced Graphs

2

2

Importing csv dataframes

setwd(“/Users/…./UC/ITS530/Rprog”)

pcd <- read.csv("dataset_price_personal_computers.csv", na.string="")

Commands:

read.csv(file, header = TRUE, sep = ",", quote = "\"", dec = ".", fill = TRUE, comment.char = "", ...)

na.string=“”

a character vector of strings which are to be interpreted as NA values. Blank fields are also considered to be missing values in logical, integer, numeric and complex fields. Note that the test happens after white space is stripped from the input, so na.strings values may need their own white space stripped in advance.

ITS530 R Advanced Graphs

3

ggplot2

Advantages of ggplot2

consistent underlying grammar of graphics (Wilkinson, 2005)

plot specification at a high level of abstraction

very flexible

theme system for polishing plot appearance

mature and complete graphics system

many users, active mailing list

That said, there are some things you cannot (or should not) do With ggplot2:

3-dimensional graphics (see the rgl package)

Graph-theory type graphs (nodes/edges layout; see the igraph package)

Interactive graphics (see the ggvis package)

ITS530 R Advanced Graphs

4

https://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html

What is ggplot2?

ITS530 R Advanced Graphs

5

Grammar of graphics:

Independently specify plot building blocks

combine them to create a graphical display to your liking.

Building blocks of a graph include:

data

aesthetic mapping

geometric object

statistical transformations

scales

coordinate system

position adjustments

faceting

What is a geom

Use a geom to

represent data points,

geom’s aesthetic properties to represent variables.

each function returns a layer.

ITS530 R Advanced Graphs

6

ITS530 R Advanced Graphs

7

ITS530 R Advanced Graphs

8

2 Variable

3 variable

ITS530 R Advanced Graphs

9

2 variable contd.

Example graph in ggplot2

You can reproduce this graphic from the Economist

ITS530 R Advanced Graphs

10

ggplot2 VS Base Graphics in R

Compared to base graphics, ggplot2

is more verbose for simple / canned graphics

is less verbose for complex / custom graphics

does not have methods (data should always be in a data.frame)

uses a different system for adding plot elements

ITS530 R Advanced Graphs

11

Simple Analysis - Base Graphs Ok

housing <- read.csv("landdata-states.csv")

hist(housing$Home.Value)

ITS530 R Advanced Graphs

12

library(ggplot2)

ggplot(housing, aes(x = Home.Value)) +

geom_histogram()

Ggplot2 - complex

https://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html#introduction

Complex graphs; ggplot easier!

ggplot(subset(housing, State %in% c("MA", "TX")), aes(x=Date, y=Home.Value, color=State))+ geom_point()

ITS530 R Advanced Graphs

13

plot(Home.Value ~ Date,

data=subset(housing, State == "MA"))

points(Home.Value ~ Date, col="red",

data=subset(housing, State == "TX"))

legend(1975, 400000,

c("MA", "TX"), title="State",

col=c("black", "red"),

pch=c(1, 1))

Geometric Objects And Aesthetics

Aesthetic Mapping

In ggplot land aesthetic means “something you can see”. Examples include:

position (i.e., on the x and y axes)

color (“outside” color)

fill (“inside” color)

shape (of points)

linetype

Size

Each geom accepts only a subset of all aesthetics

ITS530 R Advanced Graphs

14

Geometric Objects (geom)

are the actual marks we put on a plot.

Examples include:

points (geom_point, for scatter plots, dot plots, etc)

lines (geom_line, for time series, trend lines, etc)

boxplot (geom_boxplot, for, well, boxplots!)

A plot must have at least one geom;

there is no upper limit.

can add a geom to a plot using the + operator

Different Geoms

You can get a list of available geometric objects using the code below:

>help.search("geom_", package = "ggplot2")

ITS530 R Advanced Graphs

15

ggplot2::geom_abline Reference lines: horizontal, vertical, and diagonal
ggplot2::geom_bar Bars charts
ggplot2::geom_bin2d Heatmap of 2d bin counts
ggplot2::geom_blank Draw nothing
ggplot2::geom_boxplot A box and whiskers plot (in the style of Tukey)
ggplot2::geom_contour 2d contours of a 3d surface
ggplot2::geom_count Count overlapping points
ggplot2::geom_density Smoothed density estimates
ggplot2::geom_density_2d Contours of a 2d density estimate
ggplot2::geom_dotplot Dot plot
ggplot2::geom_errorbarh Horizontal error bars
ggplot2::geom_hex Hexagonal heatmap of 2d bin counts
ggplot2::geom_freqpoly Histograms and frequency polygons
ggplot2::geom_jitter Jittered points
ggplot2::geom_crossbar Vertical intervals: lines, crossbars & errorbars
ggplot2::geom_map Polygons from a reference map
ggplot2::geom_path Connect observations
ggplot2::geom_point Points
ggplot2::geom_polygon Polygons
ggplot2::geom_qq A quantile-quantile plot
ggplot2::geom_quantile Quantile regression
ggplot2::geom_ribbon Ribbons and area plots
ggplot2::geom_rug Rug plots in the margins
ggplot2::geom_segment Line segments and curves
ggplot2::geom_smooth Smoothed conditional means
ggplot2::geom_spoke Line segments parameterised by location, direction and distance
ggplot2::geom_label Text
ggplot2::geom_raster Rectangles
ggplot2::geom_violin Violin plot
ggplot2::update_geom_defaults Modify geom/stat aesthetic defaults for future plots

Points (Scatterplot) geom_point()

hp2001Q1 <- subset(housing, Date == 2001.25)

> ggplot(hp2001Q1, aes(y = Structure.Cost, x = Land.Value)) + geom_point()

ITS530 R Advanced Graphs

16

> ggplot(hp2001Q1,

aes(y = Structure.Cost, x = log(Land.Value))) +

geom_point()

Lines (Prediction Line)

#Prediction

hp2001Q1$pred.SC <- predict(lm(Structure.Cost ~ log(Land.Value), data = hp2001Q1))

ITS530 R Advanced Graphs

17

# scatter plot

p1 <- ggplot(hp2001Q1, aes(x = log(Land.Value), y = Structure.Cost))

#prediction + scatter plot

p1 + geom_point(aes(color = Home.Value)) +

geom_line(aes(y = pred.SC))

Smoothers

the smooth geom includes a line and a ribbon.

p1 + geom_point(aes(color = Home.Value)) + geom_smooth()

ITS530 R Advanced Graphs

18

Text (Label Points)

ITS530 R Advanced Graphs

19

p1 + geom_text(aes(label=State), size = 3)

## install.packages("ggrepel")

library("ggrepel")

p1 + geom_point() + geom_text_repel(aes(label=State), size = 3)

Aesthetic Mapping VS Assignment

ITS530 R Advanced Graphs

20

p1 + geom_point(aes(size = 2),# incorrect! 2 is not a variable

+ color="red") # this is fine -- all points red

p1 + geom_point(aes(color=Home.Value, shape = region))

Exercise I

The data for the exercises is available in the EconomistData.csv file. Read it in with read.csv()

It consist of the following scores for several countries:

Human Development Index (HDI) and 

Corruption Perception Index (CPI) .

Create a scatter plot with CPI on the x axis and HDI on the y axis.

Color the points blue.

Map the color of the the points to Region.

Make the points bigger by setting size to 2

Map the size of the points to HDI.Rank

ITS530 R Advanced Graphs

21

Solution I

setwd("~/Desktop/Training/UC/ITS530/RProg")

dat <- read.csv("EconomistData.csv")

# Create a scatter plot with CPI on the x axis and HDI on the y axis.

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point()

# Color the points blue.

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point(color = "blue")

# Color the points in the previous plot according to Region.

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point(aes(color = Region))

# Make the points bigger by setting size to 2

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point(aes(color = Region), size = 2)

# Map the size of the points to HDI.Rank

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point(aes(color = Region, size = HDI.Rank))

ITS530 R Advanced Graphs

22

Exercise II

Re-create a scatter plot with CPI on the x axis and HDI on the y axis (as you did in the previous exercise).

Overlay a smoothing line on top of the scatter plot using geom_smooth.

Overlay a smoothing line on top of the scatter plot using geom_smooth, but use a linear model for the predictions. Hint: see ?stat_smooth.

Overlay a smoothing line on top of the scatter plot using geom_line. Hint: change the statistical transformation.

BONUS: Overlay a smoothing line on top of the scatter plot using the default loess method, but make it less smooth. Hint: see ?loess.

ITS530 R Advanced Graphs

23

Solution II

1. Re-create a scatter plot with CPI on the x axis and HDI on the y axis (as you did in the previous exercise).

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point()

2. Overlay a smoothing line on top of the scatter plot using geom_smooth

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point() +

geom_smooth()

3. Overlay a smoothing line on top of the scatter plot using geom_smooth, but use a linear model for the predictions. Hint: see ?stat_smooth.

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point() +

geom_smooth(method = "lm")

4. Overlay a loess (method = “loess”) smoothling line on top of the scatter plot using geom_line. Hint: change the statistical transformation.

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point() +

geom_line(stat = "smooth", method = "loess")

5. Overlay a smoothing line on top of the scatter plot using the loess method, but make it less smooth. Hint: see ?loess.

ggplot(dat, aes(x = CPI, y = HDI)) +

geom_point() +

geom_smooth(span = .4)

ITS530 R Advanced Graphs

24

5. Overlay a smoothing line on top of

The scatter plot using loess method

Scales: Controlling Aesthetic Mapping

Describing what colors/shapes/sizes etc. to use is done by modifying the corresponding scale.

In ggplot2 scales include

position

color and fill

size

shape

line type

Scales are modified with a series of functions using a scale_<aesthetic>_<type> naming scheme.

Common Scale Arguments

name: the first argument gives the axis or legend title

limits: the minimum and maximum of the scale

breaks: the points along the scale where labels should appear

labels: the labels that appear at each break

ITS530 R Advanced Graphs

25

Exercise III

Create a scatter plot with CPI on the x axis and HDI on the y axis. Color the points to indicate region.

Modify the x, y, and color scales so that they have more easily-understood names (e.g., spell out “Human development Index” instead of “HDI”).

Modify the color scale to use specific values of your choosing. Hint: see ?scale_color_manual.

ITS530 R Advanced Graphs

26

Solution Exercise III

Create a scatter plot with CPI on the x axis and HDI on the y axis. Color the points to indicate region.

ggplot(dat, aes(x = CPI, y = HDI, color = "Region")) +

geom_point()

2. Modify the x, y, and color scales so that they have more easily-understood names (e.g., spell out “Human development Index” instead of “HDI”).

ggplot(dat, aes(x = CPI, y = HDI, color = "Region")) +

geom_point() +

scale_x_continuous(name = "Corruption Perception Index") +

scale_y_continuous(name = "Human Development Index") +

scale_color_discrete(name = "Region of the world")

ITS530 R Advanced Graphs

27

3. Modify the color scale to use specific values of your choosing. Hint: see ?scale_color_manual.

ggplot(dat, aes(x = CPI, y = HDI, color = "Region")) +

geom_point() +

scale_x_continuous(name = "Corruption Perception Index") +

scale_y_continuous(name = "Human Development Index") +

scale_color_manual(name = "Region of the world",

values = c("#24576D",

"#099DD7",

"#28AADC",

"#248E84",

"#F2583F",

"#96503F"))

Exercise IV – Recreate this Graph

To complete the basic scatter plot:

Add a trend line

Change the point shape to open circle

Label select points

Change the order & labels of Region;

Add title and format axes

Theme tweaks

ITS530 R Advanced Graphs

28

1. Add a trend line

Creating the basic scatter plot,

dat <- read.csv("EconomistData.csv")

library(ggplot2)

pc1 <- ggplot(dat, aes(x = CPI, y = HDI, color = Region))

pc1 + geom_point()

Adding the trend line

we need to guess at the model being on the graph.

A little bit of trial and error leads to:

pc2 <- pc1 +

geom_smooth(mapping = aes(linetype = "r2"),

method = "lm",

formula = y ~ x + log(x), se = FALSE,

color = "red")

pc2 + geom_point()

ITS530 R Advanced Graphs

29

2 Change the point shape to open circle

ITS530 R Advanced Graphs

30

pc2 + geom_point(shape = 1, size = 4)

(pc3 <- pc2 + geom_point(shape = 1, size = 2.5, stroke = 1.25))

3. Label select points

ITS530 R Advanced Graphs

31

pointsToLabel <- c("Russia", "Venezuela", "Iraq", "Myanmar", "Sudan",

"Afghanistan", "Congo", "Greece", "Argentina", "Brazil",

"India", "Italy", "China", "South Africa", "Spane",

"Botswana", "Cape Verde", "Bhutan", "Rwanda", "France",

"United States", "Germany", "Britain", "Barbados", "Norway", "Japan",

"New Zealand", "Singapore")

(pc4 <- pc3 +

geom_text(aes(label = Country),

color = "gray20",

data = subset(dat, Country %in% pointsToLabel)))

library("ggrepel")

(pc4 <- pc3 +

geom_text_repel(aes(label = Country),

color = "gray20",

data = subset(dat, Country %in% pointsToLabel),

force = 10))

4. Change the order and labels of Region;

ITS530 R Advanced Graphs

32

dat$Region <- factor(dat$Region,

levels = c("EU W. Europe",

"Americas",

"Asia Pacific",

"East EU Cemt Asia",

"MENA",

"SSA"),

labels = c("OECD",

"Americas",

"Asia &\nOceania",

"Central &\nEastern Europe",

"Middle East &\nNorth Africa",

"Sub-Saharan\nAfrica"))

pc4$data <- dat

pc4

5. Add title and format axes

ITS530 R Advanced Graphs

33

library(grid)

(pc5 <- pc4 +

scale_x_continuous(name = "Corruption Perceptions Index, 2011 (10=least corrupt)",

limits = c(.9, 10.5),

breaks = 1:10) +

scale_y_continuous(name = "Human Development Index, 2011 (1=Best)",

limits = c(0.2, 1.0),

breaks = seq(0.2, 1.0, by = 0.1)) +

scale_color_manual(name = "",

values = c("#24576D",

"#099DD7",

"#28AADC",

"#248E84",

"#F2583F",

"#96503F")) +

ggtitle("Corruption and Human development"))

6. Theme tweaks

ITS530 R Advanced Graphs

34

library(grid) # for the 'unit' function

(pc6 <- pc5 +

theme_minimal() + # start with a minimal theme and add what we need

theme(text = element_text(color = "gray20"),

legend.position = c("top"), # position the legend in the upper left

legend.direction = "horizontal",

legend.justification = 0.1, # anchor point for legend.position.

legend.text = element_text(size = 11, color = "gray10"),

axis.text = element_text(face = "italic"),

axis.title.x = element_text(vjust = -1), # move title away from axis

axis.title.y = element_text(vjust = 2), # move away for axis

axis.ticks.y = element_blank(), # element_blank() is how we remove elements

axis.line = element_line(color = "gray40", size = 0.5),

axis.line.y = element_blank(),

panel.grid.major = element_line(color = "gray50", size = 0.5),

panel.grid.major.x = element_blank()

))

7. Final tweaks

mR2 <- summary(lm(HDI ~ CPI + log(CPI), data = dat))$r.squared mR2 <- paste0(format(mR2, digits = 2), "%")

See this text file:

ITS530 R Advanced Graphs

35

Questions?

ITS530 R Advanced Graphs

36

Microsoft Word Document

Microsoft

Word Document

p <- ggplot(dat,

mapping = aes(x = CPI, y = HDI)) +

geom_smooth(mapping = aes(linetype = "r2"),

method = "lm",

formula = y ~ x + log(x), se = FALSE,

color = "red") +

geom_point(mapping = aes(color = Region),

shape = 1,

size = 4,

stroke = 1.5) +

geom_text_repel(mapping = aes(label = Country, alpha = labels),

color = "gray20",

data = transform(dat,

labels = Country %in% c("Russia",

"Venezuela",

"Iraq",

"Mayanmar",

"Sudan",

"Afghanistan",

"Congo",

"Greece",

"Argentinia",

"Italy",

"Brazil",

"India",

"China",

"South Africa",

"Spain",

"Cape Verde",

"Bhutan",

"Rwanda",

"France",

"Botswana",

"France",

"US",

"Germany",

"Britain",

"Barbados",

"Japan",

"Norway",

"New Zealand",

"Sigapore"))) +

scale_x_continuous(name = "Corruption Perception Index, 2011 (10=least corrupt)",

limits = c(1.0, 10.0),

breaks = 1:10) +

scale_y_continuous(name = "Human Development Index, 2011 (1=best)",

limits = c(0.2, 1.0),

breaks = seq(0.2, 1.0, by = 0.1)) +

scale_color_manual(name = "",

values = c("#24576D",

"#099DD7",

"#28AADC",

"#248E84",

"#F2583F",

"#96503F"),

guide = guide_legend(nrow = 1, order=1)) +

scale_alpha_discrete(range = c(0, 1),

guide = FALSE) +

scale_linetype(name = "",

breaks = "r2",

labels = list(bquote(R^2==.(mR2))),

guide = guide_legend(override.aes = list(linetype = 1, size = 2, color = "red"), order=2)) +

ggtitle("Corruption and human development") +

labs(caption="Sources: Transparency International; UN Human Development Report") +

theme_bw() +

theme(panel.border = element_blank(),

panel.grid = element_blank(),

panel.grid.major.y = element_line(color = "gray"),

text = element_text(color = "gray20"),

axis.title.x = element_text(face="italic"),

axis.title.y = element_text(face="italic"),

legend.position = "top",

legend.direction = "horizontal",

legend.box = "horizontal",

legend.text = element_text(size = 12),

plot.caption = element_text(hjust=0),

plot.title = element_text(size = 16, face = "bold"))

p