Chapter 5 Graph (W4)
시각화는 중요하다.
- Plot
- Histogram
- Dot Plots
- Bar Plots
- Line Charts
- Pie Charts
5.1 Plot
- plot(x,y)
- abline()
- title(“차트제목 입력”)
head(mtcars) #mtcars 데이터에 무엇이 있는지 보자, head()는 맨위 6개 데이터만 보여줌
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
plot(mtcars$wt, mtcars$mpg)
title("Plot test")
5.2 Histogram
- 히스토그램(histogram)
- hist()
hist(mtcars$mpg)
5.3 Dot Chart
- dotchart()
- label: y좌표 라벨링
- cex: 글자크기
- main: 차트제목
- xlab: x좌표 라벨링
기본
dotchart(mtcars$mpg)
기본 + 옵션
dotchart(mtcars$mpg, labels = row.names(mtcars), cex = 0.7,
main = "Gas Milage for Car Models", xlab = "Miles Per Gallon")
5.4 Bar Plot
- barplot()
- main: 차트제목
- xlab: x좌표 라벨링
기본
counts <- table(mtcars$gear)
barplot(counts)
기본 + 옵션적용
counts <- table(mtcars$gear)
barplot(counts, main = "Car Distribution", xlab = "Number of Gears")
5.5 Line Chart
- lines()
- type
x <- c(1:5)
y <- x # create some data
par(pch = 22, col = "red") # plotting symbol and color
par(mfrow = c(2, 4)) # all plots on one page
opts = c("p", "l", "o", "b", "c", "s", "S", "h")
for (i in 1:length(opts)) {
heading = paste("type=", opts[i])
plot(x, y, type = "n", main = heading)
lines(x, y, type = opts[i])
}
5.6 Pie Chart
- pie()
- labels
- main
기본형
myData <- c(10, 12, 4, 16, 8)
myLabels <- c("US", "UK", "Australia", "Germany", "France")
pie(myData, labels = myLabels, main = "Pie Chart of Countries")
기본 + 옵션
# Pie Chart with Percentages
myData <- c(10, 12, 4, 16, 8)
myLabels <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(myData/sum(myData) * 100)
myLabels <- paste(myLabels, pct) # add percents to labels
myLabels <- paste(myLabels, "%", sep = "") # ad % to labels
pie(myData, labels = myLabels, col = rainbow(length(myLabels)),
main = "Pie Chart of Countries")
기본형 + 옵션 (3D)
# 3D Exploded Pie Chart
library(plotrix)
myData <- c(10, 12, 4, 16, 8)
myLabels <- c("US", "UK", "Australia", "Germany", "France")
pie3D(myData, labels = myLabels, explode = 0.1, main = "Pie Chart of Countries ")
# Pie Chart from data frame with Appended Sample Sizes
mytable <- table(iris$Species)
lbls <- paste(names(mytable), "\n", mytable, sep = "")
pie(mytable, labels = lbls, main = "Pie Chart of Species\n (with sample sizes)")
5.7 Boxplots
# Boxplot of MPG by Car Cylinders
boxplot(mpg ~ cyl, data = mtcars, main = "Car Milage Data",
xlab = "Number of Cylinders", ylab = "Miles Per Gallon")
5.8 Google Chart
구글챠트 [Link]
5.8.1 Line Chart
- library(googleVis)
- data.frame(라벨링, 데이터1, 데이터2)
코드를 실행하면, 자동으로 브라우저(IE, chrome)가 실행되며 그래프를 보여줌
library(googleVis)
df = data.frame(country = c("US", "GB", "BR"), val1 = c(10,
13, 14), val2 = c(23, 12, 32))
Line <- gvisLineChart(df)
plot(Line)
5.8.2 Bar Chart
- library(googleVis)
- data.frame(라벨링, 데이터1, 데이터2)
코드를 실행하면, 자동으로 브라우저(IE, chrome)가 실행되며 그래프를 보여줌
library(googleVis)
df = data.frame(country = c("US", "GB", "BR"), val1 = c(10,
13, 14), val2 = c(23, 12, 32))
Bar <- gvisBarChart(df)
plot(Bar)