0% found this document useful (0 votes)
8 views20 pages

5_SingleVariableVisualization_2

The document provides an overview of single variable visualization techniques using R, including pie charts, point plots, line plots, and bar plots. It explains the basic functions and arguments for each type of plot, as well as how to add legends and aggregate data for visual representation. Additionally, it includes exercises for practicing these visualization methods.

Uploaded by

shahbozxon
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)
8 views20 pages

5_SingleVariableVisualization_2

The document provides an overview of single variable visualization techniques using R, including pie charts, point plots, line plots, and bar plots. It explains the basic functions and arguments for each type of plot, as well as how to add legends and aggregate data for visual representation. Additionally, it includes exercises for practicing these visualization methods.

Uploaded by

shahbozxon
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
You are on page 1/ 20

Single Variable Visualization 2

Keunwoo Kim

2019 Fall

Keunwoo Kim Single Variable Visualization 2 2019 Fall 1 / 20


Pie Charts

Basic Function
A data vector and label information.

pie(c(3.5,1,4), labels=c("A","B","C"))
A

Keunwoo Kim Single Variable Visualization 2 2019 Fall 2 / 20


Pie Charts

More Arguments
"col" and "main"
pie(c(3.5,1,4), labels=c("A","B","C"), col=c("red","blue","yellow"),
main="Pie Chart for A, B, and C")
Pie Chart for A, B, and C

Keunwoo Kim Single Variable Visualization 2 2019 Fall 3 / 20


Pie Charts
Pie Chart + Contingency Table
The output from "table" can be directly used to draw a pie chart.

pie(table(Store$country), col=rainbow(7))
CN

DE
BR

AU

GB US

JP

The “rainbow(7)” function automatically generates 7 rainbow colors.


Keunwoo Kim Single Variable Visualization 2 2019 Fall 4 / 20
Point Plots

Basic Function
A data vector is used to draw a series of points.

vec = c(15, 12, 16, 18, 21, 17, 14, 13, 15, 11)
plot(vec)
20
18
vec

16
14
12

2 4 6 8 10

Index

Keunwoo Kim Single Variable Visualization 2 2019 Fall 5 / 20


Point Plots

"pch" Argument
Changes the type of points.

vec = c(15, 12, 16, 18, 21, 17, 14, 13, 15, 11)
plot(vec, pch=2) 20
18
vec

16
14
12

2 4 6 8 10

Index

Try different numbers for the “pch” argument.

Keunwoo Kim Single Variable Visualization 2 2019 Fall 6 / 20


Point Plots

More Arguments
Common arguments used for visualization.

vec = c(15, 12, 16, 18, 21, 17, 14, 13, 15, 11)
plot(vec, pch=20, main="Points", ylab="Values", col="blue")
Points
20
18
Values

16
14
12

2 4 6 8 10

Index

Keunwoo Kim Single Variable Visualization 2 2019 Fall 7 / 20


Line Plots

Basic Function
Use "plot" function with the argument type="l".

vec = c(15, 12, 16, 18, 21, 17, 14, 13, 15, 11)
plot(vec, type="l", main="Line Plot", ylab="Values", col="red")
Line Plot
20
18
Values

16
14
12

2 4 6 8 10

Index

Keunwoo Kim Single Variable Visualization 2 2019 Fall 8 / 20


Line Plots
"lwd" and "lty" Arguments
"lwd" and "lty" change the width and the type of lines.

vec = c(15, 12, 16, 18, 21, 17, 14, 13, 15, 11)
plot(vec, type="l", lwd=7, lty=2,
main="Line Plot", ylab="Values", col="red")
Line Plot
20
18
Values

16
14
12

2 4 6 8 10

Index

Try different numbers for the “lty” arguments.


Keunwoo Kim Single Variable Visualization 2 2019 Fall 9 / 20
Drawing Multiple Lines

"lines" Function
"lines" function adds a line to an pre-existing plot.

# data for store 101 during year 1


Store101 = Store[Store$storeNum==101 & Store$Year==1,]
# data for store 102 during year 1
Store102 = Store[Store$storeNum==102 & Store$Year==1,]

# creating a new line plot: store 101


plot(Store101$p1sales, type="l", lwd=2, col="blue",
main="p1sales of Store 101 and 102",
xlab="Week Number", ylab="Unit Sales (Product 1)",
ylim=c(0,250))
# adding a line for store 102 to store 101 plot
lines(Store102$p1sales, type="l", col="red", lwd=5)

Keunwoo Kim Single Variable Visualization 2 2019 Fall 10 / 20


Drawing Multiple Lines

p1sales of Store 101 and 102


250
200
Unit Sales (Product 1)

150
100
50
0

0 10 20 30 40 50

Week Number

Keunwoo Kim Single Variable Visualization 2 2019 Fall 11 / 20


Adding Legends
"legend" Function
"legend" function explains what each of lines or points means.

# data for store 101 during year 1


Store101 = Store[Store$storeNum==101 & Store$Year==1,]
# data for store 102 during year 1
Store102 = Store[Store$storeNum==102 & Store$Year==1,]

# creating a new line plot: store 101


plot(Store101$p1sales, type="l", lwd=2, col="blue",
main="p1sales of Store 101 and 102", ylim=c(0,250),
xlab="Week Number", ylab="Unit Sales (Product 1)")
# adding a line for store 102 to store 101 plot
lines(Store102$p1sales, type="l", col="red", lwd=5)

# adding legends
legend("topright", legend=c("Store101","Store102"),
lty=c(1,1), col=c("blue","red"), lwd=c(2,5))
Keunwoo Kim Single Variable Visualization 2 2019 Fall 12 / 20
Adding Legends

p1sales of Store 101 and 102


250
Store101
Store102
200
Unit Sales (Product 1)

150
100
50
0

0 10 20 30 40 50

Week Number

Keunwoo Kim Single Variable Visualization 2 2019 Fall 13 / 20


Bar Plots

Basic Function
A data vector and the names of bars.

vec = c(15, 12, 16, 18)


barplot(vec, names=c("A","B","C","D"))
15
10
5
0

A B C D

Keunwoo Kim Single Variable Visualization 2 2019 Fall 14 / 20


Bar Plots

Bar Plot + Contingency Table


The output from "table" can be directly used to draw a bar plot.

barplot(table(Store$country))
500
400
300
200
100
0

AU BR CN DE GB JP US

Keunwoo Kim Single Variable Visualization 2 2019 Fall 15 / 20


Aggregate by Columns

"aggregate" Function
The way to use is quite similar as for the "ftable" and "boxplot" functions.

data = data.frame(A=c(2,0,1,0,1,1,2), B=c(2,0,6,1,3,10,2))


# total sum of B for each value of A
aggregate(B ~ A, data=data, FUN=sum)

## A B
## 1 0 1
## 2 1 19
## 3 2 4

Keunwoo Kim Single Variable Visualization 2 2019 Fall 16 / 20


Aggregate by Columns

Another Example
Average unit sales (product 1) for each country.

aggregate(p1sales ~ country, data=Store, FUN=mean)

## country p1sales
## 1 AU 139.8462
## 2 BR 133.8269
## 3 CN 131.6394
## 4 DE 132.4538
## 5 GB 131.3654
## 6 JP 133.1274
## 7 US 133.7724

Keunwoo Kim Single Variable Visualization 2 2019 Fall 17 / 20


Bar Plots

Bar Plot + "aggregate"


The output from "aggregate" can be directly used to draw a bar plot.

# total unit sales (product 1) for each country


p1sales_by_country = aggregate(p1sales ~ country,
data=Store, FUN=sum)

# draw bar plot


barplot(p1sales_by_country$p1sales,
names=p1sales_by_country$country, col=rainbow(7))
# add legends to the bar plot
legend("topleft", legend=p1sales_by_country$country,
fill=rainbow(7))

Keunwoo Kim Single Variable Visualization 2 2019 Fall 18 / 20


Bar Plots

AU
BR
60000

CN
DE
GB
JP
50000

US
40000
30000
20000
10000
0

AU BR CN DE GB JP US

Keunwoo Kim Single Variable Visualization 2 2019 Fall 19 / 20


Exercise

The purpose of this exercise is to complete the below table and visualize it.
1 Using aggregate and cbind functions, complete the below table.
2 Display four bar plots in one window, including (1) minimum, (2)
mean, (3) median, and (4) maximum price per carat information for
each of the four classes from the table.
Color Clarity Pr./Car. (Min) Pr./Car. (Mean) Pr./Car. (Med.) Pr./Car. (Max)
D IF ? ? ? ?
D Lower than IF ? ? ? ?
Lower than D IF ? ? ? ?
Lower than D Lower than IF ? ? ? ?

Keunwoo Kim Single Variable Visualization 2 2019 Fall 20 / 20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy