第四回R勉強会
Rで作った感動したグラフを紹介
1. plot
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 "setosa"
2 4.9 3 1.4 0.2 "setosa"
3 4.7 3.2 1.3 0.2 "setosa"
4 4.6 3.1 1.5 0.2 "setosa"
5 5 3.6 1.4 0.2 "setosa"
6 5.4 3.9 1.7 0.4 "setosa"
7 4.6 3.4 1.4 0.3 "setosa"
8 5 3.4 1.5 0.2 "setosa"
9 4.4 2.9 1.4 0.2 "setosa"
10 4.9 3.1 1.5 0.1 "setosa"
1. plot
plot(x,y, ...)
> plot(iris[,"Sepal.Length"],iris[,"Petal.Length"])
1. plot
plot(iris[,"Sepal.Length"],iris[,"Petal.Length"],
xlab = "Sepal Length", ylab = "Petal Length",
main = "Iris data: Sepal vs. Petal Length")
1. plot
plot(iris[,"Sepal.Length"],iris[,"Petal.Length"],
xlab = "Sepal Length", ylab = "Petal Length",
main = "Iris data: Sepal vs. Petal Length",
col=c("orange3","seagreen4"))
1. plot
plot(iris[,"Sepal.Length"],iris[,"Petal.Length"],
xlab = "Sepal Length", ylab = "Petal Length",
main = "Iris data: Sepal vs. Petal Length",
col=c("orange3","seagreen4"))
par(bty="l",las=1,bg="antiquewhite1")
1. plot
plot(iris[,"Sepal.Length"],iris[,"Petal.Length"],
xlab = "Sepal Length", ylab = "Petal Length",
main = "Iris data: Sepal vs. Petal Length",
col=c("orange3","seagreen4"))
legend("bottomright",legend=c("Sepal Length","Petal Length"),
fill=c("orange3","seagreen4"),ncol=1,title="Iris data legend")
●
ggplot2でデータフレームは中心になりました
● 色と大きさと形でデータの属性を表せる
●
ggplot2のグラフは三つのレイヤーで作れる:
– Data layer (データフレーム)
– Graphics layers (点や線など )
– Statistic layers
●
qplotとggplotの関数でグラフを作ります
2. ggplot2
2. ggplot2
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 "setosa"
2 4.9 3 1.4 0.2 "setosa"
3 4.7 3.2 1.3 0.2 "setosa"
4 4.6 3.1 1.5 0.2 "setosa"
5 5 3.6 1.4 0.2 "setosa"
6 5.4 3.9 1.7 0.4 "setosa"
7 4.6 3.4 1.4 0.3 "setosa"
8 5 3.4 1.5 0.2 "setosa"
9 4.4 2.9 1.4 0.2 "setosa"
10 4.9 3.1 1.5 0.1 "setosa"
2. ggplot2
● qplot(Sepal.Length,Petal.Length,data=iris)
● qplot(Sepal.Length,Petal.Length,data=iris,color=Species)
● qplot(Sepal.Length,Petal.Length,data=iris,color=Species,size=Petal.Width)
● qplot(Sepal.Length,Petal.Length,data=iris,color=Species,size=Petal.Width,alpha=I(0.7))
2. ggplot2
● qplot(Sepal.Length,Petal.Length,data=iris,color=Species)+geom_rug()
● qplot(Sepal.Length,Petal.Length,data=iris,color=Species)+geom_line()
● qplot(Sepal.Length,Petal.Length,data=iris,color=Species)+stat_smooth()
2. ggplot2
labels vals
79 rnorm(mean=0,sd=0) 0.7996605
396 rexp(rate=0.3) 9.3830294
240 rnorm(mean=1,sd=0.5) 0.9357393
194 rnorm(mean=-3,sd=3) 1.1291675
200 rnorm(mean=-3,sd=3) 6.1559452
245 rnorm(mean=1,sd=0.5) 1.0050954
…....
> head(xy)
> qplot(vals,data=xy,col=labels,fill=labels,alpha=I(.3),size=I(.8),geom="density")
3. shiny
●
Node.jsを使って統計的なア
プリをWEBにアップことが
できる
● 対話的なデータアプリを目
指す
●
サーバとUIはずっとR言語
で作れる
3. shiny
shinyUI(pageWithSidebar(
headerPanel(),
sidebarPanel(),
mainPanel()
))
headerPanel
sidebarPanel
mainPanel
3. shiny
shinyUI(pageWithSidebar(
headerPanel("Benkyokai app"),
...
))
Benkyoukai app
3. shiny
shinyUI(pageWithSidebar(
...
sidebarPanel(
numericInput(inputId = "n_points",label = "Number points randomized",value=100),
...),
numericInput
3. shiny
shinyUI(pageWithSidebar(
...
sidebarPanel(
numericInput(inputId = "n_points",label = "Number points randomized",value=100),
checkboxInput(inputId = "dnorm",
label = "Show normal distribution graph",
value = TRUE),
...),
numericInput
checkboxInput
3. shiny
shinyUI(pageWithSidebar(
...
sidebarPanel(
numericInput(inputId = "n_points",label = "Number points randomized",value=100),
checkboxInput(inputId = "dnorm",
label = "Show normal distribution graph",
value = TRUE),
conditionalPanel(condition = "input.dnorm == true",
numericInput(inputId = "norm_mean",label = "Mean",value=0),
numericInput(inputId = "norm_std_dev",label = "Standard deviation",value=1)),
...),
numericInput
checkboxInput
conditionalPanel
numericInput
numericInput
3. shiny
shinyUI(pageWithSidebar(
...
),
mainPanel(plotOutput(outputId = "main_plot",width="750px", height = "500px"))
...
))
mainPanel
PlotOutput
3. shiny
3. shiny
shinyServer(function(input, output) {
output$main_plot <- renderPlot({
plot.new()
plot.window(c(-3,3),c(0,1),main="fds")
axis(2,at=seq(0,1,by=0.2))
axis(1,at=seq(-5,5,by=0.5))
if(input$dnorm){
lines(density(rnorm(input$n_points,input$norm_mean,input$norm_std_dev)),
col="blue")
}
if(input$dexp){
lines(density(rexp(input$n_points,input$exp_rate)),col="green")
}
if(input$dgamma){
lines(density(rgamma(input$n_points,input$gamma_shape)),col="red")
}
})
})
3. shiny
4. 続き
ビッグデータを表
す:
グラフを表
す:
地理的なデー
タ:
qgraphパッケー
ジ
bigvisパッケージ
ggmapパッケー
ジ
一緒にR言語を勉強しましょう!

関東第3回ゼロはじめるからR言語勉強会ー グラフ