library(ggplot2)
library(plyr)
library(tidyverse)
library(gridExtra)
ggplot( data=iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species) ) +
geom_point( size=3 )
【圖】
plotData <- tribble( ~level, ~mean, ~cil, ~ciu,
1, 38.72, 38.1, 39.34,
2, 45.23, 41.6, 48.82,
3, 51.75, 48.7, 54.75,
4, 61.66, 61.1, 62.20
)
plotData %>%
ggplot() +
geom_point( aes(x=level, y=mean) ) +
geom_errorbar( aes(x=level, ymin=cil, ymax=ciu) )
- 探索類別
- 已經統計過的資訊,繪製長條圖就必須要指定一個參數
stat="identity"
TFMqe <- data.frame( stringsAsFactors=FALSE,
Group=c( "Non-Fertile Chip",
"Non-Fertile Chip","Fertile Chip","Fertile Chip",
"Non-Fertile Chip","Fertile Chip","Non-Fertile Chip",
"Non-Fertile Chip","Fertile Chip","Non-Fertile Chip"),
NTopQualityEmbryos=c("Yes","Yes","Yes","Yes",
"Yes","No","Yes","Yes","No","No")
)
ggplot( TFMqe, aes(x=NTopQualityEmbryos,
y=stat(count/sum(count)),
fill=Group) ) +
geom_bar( position=position_dodge2()) +
geom_text( aes(label=scales::percent(stat(count/sum(count)),
accuracy=0.01)),
position=position_dodge2(.9),
vjust=1.6,
color="white",
size=3.5,
stat="count" ) +
scale_y_continuous( label=scales::label_percent() ) +
labs( x="NTopQualityEmbryos",
y="Percentatges",
fill="Group" ) +
theme_minimal() +
theme( legend.position = "bottom" )
iris.mean <- aggregate( iris[,1:4], by=list(Species=iris$Species), FUN=mean )
mydata <- cbind( stack(iris.mean[,-1]), Species=iris.mean$Species )
ggplot( mydata, aes(x=ind, y=values, fill = Species) ) + geom_bar( position="dodge", stat="identity" )
【圖】
- 探索不同類別與數值分佈的關係
mtcars$cyl <- factor(mtcars$cyl)
ggplot(data=mtcars, aes(x=cyl, y=disp)) + geom_boxplot()
【圖】
- 探索數值分佈
prediction_1 = 0.469
prediction_3 <- data.frame( prediction_3=rnorm(1000, 4, 3) )
prediction_2b <- data.frame( prediction=rnorm(10000, 8, 3) )
ggplot( prediction_3 ) +
geom_histogram( aes(x=prediction_3), binwidth=0.01 )
ggplot(prediction_2b) +
geom_histogram( aes(x=prediction), binwidth=0.01 )
dats <- rbind( data.frame(pred=prediction_3$prediction_3, var='prediction_3'),
data.frame(pred=prediction_2b$pred, var='prediction_2b') )
ggplot( dats, aes(pred, fill=var) ) +
geom_histogram( alpha=0.5, position="identity", bins=75 ) +
geom_vline( xintercept=prediction_1 )
h1 <- ggplot( data=iris, aes(x=Sepal.Length) ) + geom_histogram( )
h2 <- ggplot( data=iris, aes(x=Sepal.Length) ) + geom_histogram( binwidth=1 )
h3 <- ggplot( data=iris, aes(x=Sepal.Length) ) +
geom_histogram( color="black", fill="blue", bins=10 )
h4 <- ggplot( data=iris, aes(x=Sepal.Length, color=Species) ) + geom_histogram( binwidth=1 )
grid.arrange( h1, h2, h3, h4, nrow=1, ncol=4 )
h1 + facet_grid( Species~. ) # row
【圖】
- 探索兩個數值的關係
p <- ggplot( data=mtcars, aes(x=wt, y=mpg, label=rownames(mtcars)) )
p + geom_point() #以點呈現
p + geom_text( size=3 ) #以字呈現
p + geom_label()
【圖】
- 探索數值與日期 (時間) 的關係
p <- ggplot( data=iris, aes(x=Sepal.Length, y=Sepal.Width, shape=Species, color=Species) )+
geom_point()
p + geom_line( aes(y=Sepal.Width) )
stat_function( fun, geom = "line" )
require(raster)
require(sp)
require(lattice)
require(grid)
north <- list( "SpatialPolygonsRescale", layout.north.arrow(type=1), offset=c(0.95,0.85), scale=0.1 )
scale <- list( "SpatialPolygonsRescale", layout.scale.bar(),
offset=c(0.55, 0.03), scale=0.4, fill=c("transparent","black") )
txt1 <- list( "sp.text", c(0.55, 0.08), "0" )
txt2 <- list( "sp.text", c(0.75, 0.08), "0.2" )
txt3 <- list( "sp.text", c(0.95, 0.08), "0.4" )
raster_layout <- list( north, scale, txt1, txt2, txt3 )
cuts <- c( 110, 120, 130, 140, 150, 160, 170, 180, 190, 200 )
spplot( raster(volcano), scales=list(draw=T), at=cuts, col.regions=palette(gray(seq(0,0.9,len=9))), sp.layout=raster_layout, add=T )
grid.text( "XXX (m)", x=unit(0.95, "npc"), y=unit(0.50, "npc"), rot=-90 )
#(NOT RUN)
spplot( county2010, "grouping", col.regions=col,
colorkey=list(at=seq(0, 4, 1), labels=as.character(c( "0", "1", "2", "3", "4" )),
col=col, space="left", height=0.4),
main=name.title, xlim=c(118,124), ylim=c(21.6,26.3) )
- Shiny 主要特色:將資料分析結果轉換成互動式的網頁應用程式
- 使用者介面 (User Interface):通常會使用
dashboardPage()
函數建構,而非常見的fluidPage()
函數- 儀表板標頭 (header):
dashboardHeader()
,不需要也可關掉disable=TRUE
- 標題:
title
- 訊息選單:
messageItem()
- 通知選單:
notificationItem()
- 工作選單:
taskItem()
- 若想在後端修改選單元件,則需使用
dropdownMenuOutput()
設定輸出元件,並在伺服器端程式用renderMenu()
作動態設定
- 標題:
- 側邊選單 (sidebar):
dashboardSidebar()
- 每個選單
sidebarMenu()
中可有多個選項meunItem()
- 在選項中除可在第一個參數設定選項名稱外,也必須用
tabName
參數設定選項ID,以及icon
參數加上圖示
- 每個選單
- 主頁面 (body):
dashboardBody()
,不需要也可關掉disable=TRUE
- 使用
tabItems()
來排版 - 在
tabItems()
中可用多個tabItem()
來對應側邊欄位的每個選項,對應方式為設定相同的tabName
- 在
tabItems()
中,再使用列fluidRow
和欄位column
或資訊區塊box
的方式將圖表排版
- 使用
- 儀表板標頭 (header):
- 伺服器端程式 (Server Instruction)
- 資料呈現方式:
- 數字與文字:
renderText()
- 表格:
renderTable()
,DT套件支援互動式表格 - 圖片:
renderPlot()
- 地圖:
ggmap
套件或leaflet
套件支援互動式地圖
- 數字與文字:
- 資料呈現方式:
- 重要議題:
- 快速將資料載入:考慮批次方式,先作預處理,降低資料量,以加快資料存取速度