Visualization

OSSF X Taiwan R user Group
陳奎銘(Benjamin)
benjamin0901@gmail.com
2013.11.16

What? Report?


本圖取自於網路,本簡報無此受權

Ha! I have R!


本圖取自於網路,本簡報無此受權

Basic

[[[]]]=sud

Data

  1. 股票
  2. R data set: iris
  3. 大專校院境外學生人數統計
  4. 大專院校學生人數

iris

[[[]]]=eud

[[[]]]=sud

Plot

Plot

Plot

Plot

Plot

[[[]]]=eud

Hist

hist(iris[, 1], cex.axis = 1.5, cex.lab = 1.5, col.lab = "red", main = "hist(iris[,1])", 
    cex.main = 2)

Boxplot

boxplot(Sepal.Length ~ Species, data = iris, main = "boxplot(Sepal.Length~Species,data=iris)", 
    cex.main = 2)

[[[]]]=sud

Barplot

iris.tab = table(iris.sam)
iris.tab
## iris.sam
##     setosa versicolor  virginica 
##         14         17         19
barplot(iris.tab)

Barplot

Barplot

##    教育學門 藝術學門
## 男    13727     8213
## 女    25955    20027

Barplot

Barplot

Barplot

[[[]]]=eud

Pie

##    Asia  Europe Oceania America  Africa 
##   51855    4624     600    6577     902

Image+Contour

Persp

[[[]]]=sud

Add something

Lines

Points

text(x,y,string)

Legend

abline

segments

Arrows

[[[]]]=eud

[[[]]]=sud

Adjust

  1. Permanent Changes
  2. Temporary Changes
  3. Graphical Elements
  4. Figure Margins
  5. Multiple Figure Environment

Graphical Elements

http://spark.rstudio.com/kmchen/adjust/

Figure Margins


par(mfrow = c(2, 2))
par(mar = c(3, 1, 1, 1))  #mar controls lines of margin
plot(rnorm(100))
par(mar = c(1, 3, 1, 1))
plot(rnorm(100))
par(mai = c(1, 1, 2, 1))  #mai controls margin size in inches
plot(rnorm(100))
par(mai = c(1, 1, 1, 2))
plot(rnorm(100))

Figure Margins

Figure Margins

par(mfrow = c(2, 2), omi = c(2, 2, 2, 2))  #omi controls figure margin
par(mar = c(5, 0, 0, 0))  #mar controls lines of margin
plot(rnorm(100))
par(mar = c(0, 8, 0, 0))
plot(rnorm(100))
par(mai = c(0, 0, 1, 0))  #mai controls margin size in inches
plot(rnorm(100))
par(mai = c(0, 0, 0, 1))
plot(rnorm(100))

Figure Margins

Multiple Figure Environment


本圖取自於網路,本簡報無此受權。from: http://booklive.jp/resources/cms/feature/custom/100432/nrscene_02.png

Multiple Figure Environment

nf <- layout(matrix(c(1, 6, 2, 6, 3, 6, 4, 6, 5, 6), 5, 2, byrow = TRUE), c(5, 
    5), c(1, 1, 1, 1, 1), TRUE)
par(cex = 3, oma = c(0, 0, 0, 0))
layout.show(nf)

Multiple Figure Environment

[[[]]]=eud

[[[]]]=sud

Example 1

load("/home/ben/qftrader/plot_demo_3.RData")
kmeans_model = kmeans(TWII_20days_close_normal, 5)
x = as.numeric(table(kmeans_model$cluster))
x = round(x/sum(x) * 100)
y = paste(as.character(x), "%", sep = "")
color = c("gray22", "deepskyblue4", "firebrick2", "springgreen4", "orange")
nf <- layout(matrix(c(1, 6, 2, 6, 3, 6, 4, 6, 5, 6), 5, 2, byrow = TRUE), c(5, 
    5), c(1, 1, 1, 1, 1), TRUE)
for (i in seq(5, 1, -1)) {
    par(mar = c(1, 2, 1, 1))
    plot(kmeans_model$centers[i, ], type = "l", col = color[i], cex.axis = 1.25, 
        lwd = 2)
}
par(mar = c(1, 3, 2, 1))
barplot(x, col = color, space = 0, horiz = TRUE, legend.text = y, args.legend = list(x = 35, 
    y = 5, bty = "n", cex = 2))

Example 1

[[[]]]=eud

[[[]]]=sud

Example 2

par(mfrow = c(2, 1))
x = rnorm(100)
y = rnorm(100)
all = hist(c(x, y), plot = F)
z = all$breaks
x = hist(x, breaks = z, plot = F)$counts
y = hist(y, breaks = z, plot = F)$counts

par(mar = c(0, 2, 2, 1))
barplot(x, space = c(0, 0))

par(mar = c(2, 2, 0, 1))
barplot(-y, axes = F, space = c(0, 0))
axis(2, seq(-20, 0, 5), labels = c(as.character(seq(20, 5, -5)), ""))
axis(1, 0:(length(z) - 1), labels = as.character(z))

Example 2

[[[]]]=eud

ggplot2

ggplot2


1. data.frame

  1. nice default
  2. Layer
  3. Stack Over
  4. install.packages(“ggplot2”)
  5. library(ggplot2)

Point

qplot(year,total,data=CS_student,geom='point')
ggplot(CS_student,aes(x=year,y=total))+geom_point()

Line

qplot(year,total,data=CS_student,geom=c('point','line'))
ggplot(CS_student,aes(x=year,y=total))+geom_point()+geom_line()

Barplot

qplot(factor(year),total,data=CS_student,geom=c('bar'),stat="identity")
ggplot(CS_student,aes(x=factor(year),y=total))+geom_bar(stat='identity')

Barplot

qplot(factor(year),total,data=CS_student,geom=c('bar'),stat="identity",fill=factor(year))
ggplot(CS_student,aes(x=factor(year),y=total))+geom_bar(stat='identity',aes(fill=factor(year)))

Hist

qplot(Sepal.Length,data=iris,binwidth=0.2)
ggplot(iris,aes(x=Sepal.Length))+geom_histogram(binwidth=0.2)

Boxplot

qplot(Species,Sepal.Length,data=iris,geom='boxplot',cex.lab=2)
ggplot(iris,aes(x=Species,y=Sepal.Length))+geom_boxplot()

[[[]]]=sud

Pie

Pie

ggplot(foriegn, aes(x = "", y = foriegn, fill = area)) + geom_bar(width = 1, 
    stat = "identity") + scale_fill_manual(values = c("green", "blue", "cyan", 
    "red", "orange")) + coord_polar("y", start = pi/3) + labs(title = "Foriegn Students")

[[[]]]=eud

[[[]]]=sud

Tile+Contour

Tile+Contour

library(reshape2)
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")
ggplot(volcano3d, aes(x, y, z = z)) + geom_tile(aes(fill = z)) + stat_contour() + 
    scale_fill_gradient2(low = "red", mid = "yellow", high = "white", midpoint = 144.5)

[[[]]]=eud

Save

1. jpeg(filename=‘xxx.jpeg’,width=1252,height=623)
png, bmp, tiff, pdf 2. plot something 3. dev.off()

googleVis

googleVis


1. R & Google Charts API 2. Visualize data with Google Chart Tools without manually upload data to Google. 3. Output is html code. 4. Need browser and internet connection. 5. Tutorial 6. See also: Using the Google Visualisation API with R, The R Journal, 3(2):40-44, December 2011 and googleVis package manual.

Pie

Pie <- gvisPieChart(foriegn,options=list(width=1000, height=500))
plot(Pie)

GeoChart

Geo=gvisGeoChart(foriegn_student[-1:-2,], locationvar="area", colorvar="number",options=list(width=1000, height=500))
plot(Geo) #大專校院境外學生人數統計

Motion

Motion=gvisMotionChart(area_total, idvar="area", timevar="year")
plot(Motion)

shiny

shiny


1. beta version: 2012/11 2. 只要幾行程式碼 3. 幾乎只要會R就行 4. 動態展示 5. Tutorial Install & Run — install.packages(“shiny”) library(shiny) runExample(“01_hello”) Hello shiny! — “r sdir <- paste(system.file("examples", package = "shiny"), "01_hello", sep = "/") sdir ``` ``` ## [1] "/home/ben/R/library/shiny/examples/01_hello" ``` ```r dir(sdir) ``` ``` ## [1] "server.R" "ui.R" ``` runApp("/home/ben/R/library/shiny/examples/01_hello") ui --- ```r library(shiny) # Define UI for application that plots random distributions shinyUI(pageWithSidebar( # Application title headerPanel("Hello Shiny!"), # Sidebar with a slider input for number of observations sidebarPanel( sliderInput("obs", "Number of observations:", min = 0, max = 1000, value = 500) ), # Show a plot of the generated distribution mainPanel( plotOutput("distPlot") ) )) ``` server --- ```r library(shiny) # Define server logic required to generate and plot a random distribution shinyServer(function(input, output) { # Expression that generates a plot of the distribution. The expression is # wrapped in a call to renderPlot to indicate that: # # 1) It is 'reactive' and therefore should be automatically re-executed # when inputs change 2) Its output type is a plot output$distPlot <- renderPlot({ # generate an rnorm distribution and plot it dist <- rnorm(input$obs) hist(dist) }) }) ``` Procedure --- 1. ui.R 中的sidebarPanel提供input 2. input傳至server.R 3. 在server.R的rederPlot處理 4. 傳出output到mainPanel shiny+googlVis ---

shiny+googlVis

遇到問題時…

本圖取自於網路,本簡報無此受權

請愛用!

- help - demo - example - tutorial - google

Taiwan R User Group

Thank you!