1.plotly包
動態散點圖
library(plotly)
# 交互散點圖
plot_ly(data=iris,
x=~Sepal.Length,
y=~Petal.Length,
marker=list(size=10,
color='rgba(255,182,193,.9)',
line=list(color='rgba(152,0,0,.8)',
width=2))) %>%
layout(title='Styled Scatter',
yaxis=list(zeroline=FALSE),
xaxis=list(zeroline=FALSE))
#多維數據,添加標簽
plot_ly(mpg,x=~hwy,y=~displ,color=~factor(cyl),
text=~paste('Model:',model)) %>%
layout(title='MPG data',font=list(family='Times New Roman',
size=13,color='forestgreen'))
#參數
plot_ly(data=iris,x=~Sepal.Length,y=~Petal.Length,
type = 'scatter',#lines/markers/text/none
mode='makers',
symbol = ~Species,
#點類型
symbols = c('circle','x','o'),color = I('black'),
marker=list(size=10))
#點連線形式
trace0 <- rnorm(100,mean=5)
trace1 <- rnorm(100,mean=0)
trace2 <- rnorm(100,mean=-5)
x <- c(1:100)
data <- data.frame(x,trace0,trace1,trace2)
#第一張散點圖以線圖形式展示
plot_ly(data,x=~x,y=~trace0,
name = 'trace 0',type = 'scatter',
mode='lines') %>%
#第二張以點連線形式展示
add_trace(y=~trace1,name='trace 1',
mode='lines+markers') %>%
#第三張普通散點圖
add_trace(y=~trace2,name='trace 2',
mode='markers') %>%
layout(xaxis=list(zeroline=FALSE))
#add_trace/add_histogram/add_boxplot/add_text
其他動態圖
#動態氣泡圖
# 動態線圖
# 動態條形圖
# 動態直方圖
# 動態盒形圖
# 動態誤差條圖
# 動態餅圖和戒指圖
2. recharts包
接近基礎繪圖語法。
散點圖
#devtools::install_github('taiyun/recharts')
library(recharts)
#散點圖
ePoints(iris[,3:5],series = ~Species)
ePoints(iris[,3:5],
xvar=~Petal.Length,
yvar=~Petal.Width,
series = ~Species,
xlab.name = 'Petal Length',
ylab.name = 'Petal Width',
xlab.namePosition = 'end',
ylab.namePosition = 'end',
title = 'IRIS data',
title.x = 'center',
title.y = 'top',
legend.orient = 'vertical',
legend.x = 'right',
legend.y = 'center')
其他動態圖
#線圖
names(mtcars)
eLine(mtcars,xvar = ~cyl,yvar = ~mpg,series = ~gear)
#條形圖
eBar(mtcars,xvar = ~cyl,yvar = ~mpg,series = ~gear)
#餅圖
x=runif(6)
names(x)=LETTERS[1:6]
ePie(x,type='pie',title = 'PIE')
#玫瑰圖
ePie(x,type = 'rose',roseType = 'radias',title = 'Rose plot')
#雷達圖
eRadar(mtcars,xvar = ~cyl,yvar = ~mpg,series = ~gear)
#漏斗圖、面積圖等。。。
3. rChart包
語法近似於lattice繪圖系統。在R中實現Polychart、Morris、NVD3等多個js繪圖庫。
https://github.com/ramnathv/rCharts
沒安裝上
Ploychart
4.threejs包
3D顯示功能。
#install.packages('threejs')
library(threejs)
#三維散點圖
N <- 100
i <- sample(3,N,replace = TRUE)
x <- matrix(rnorm(N*3),ncol=3)
lab <- c('small','bigger','biggest')
scatterplot3js(x,color = rainbow(N),labels=lab[i],
size=i,renderer = "canvas")
#三維地圖
library(maps)
data("world.cities",package = 'maps')
cities <- world.cities[order(world.cities$pop,decreasing = TRUE)[1:1000],]
value <- 100*cities$pop/max(cities$pop)
col <- colorRampPalette(c('cyan','lightgreen'))(10)[floor(10*value/100)+1]
threejs::globejs(lat = cities$lat,
long=cities$long,
value=value,
color = col,
atmosphere = TRUE)
5.timevis包
#繪制動態時間軸,展示時間節點事件
library(timevis)
timevis(data.frame(id=1:3,
content=c('one','two','three'),
start=c('2016-01-10','2016-01-14','2016-01-19'),
end=c(NA,'2016-01-18',NA),
type=c('point','background','box')))
6.dygraphs包
#動態時間序列
library(dygraphs)
lungDeaths <- cbind(mdeaths,fdeaths)
dygraph(lungDeaths)
#選擇特定時間
dygraph(lungDeaths) %>%
dyRangeSelector(dateWindow = c('1975-06-01','1978-12-31'))
7.leaflet包
js開源交互式地圖包,在R中利用哦html5顯示。
#install.packages('leaflet')
library(leaflet)
f <- leaflet() #初始化
y <- addTiles(f) #調用地圖底圖
addMarkers(y,
lng = 121.445,
lat=31.1980,
popup = '復旦大學上海醫學院') #添加標記
#用管道符
leaflet() %>% addTiles() %>%
addMarkers(lng = 121.445,lat = 31.1980,popup = '復旦大學醫學院')
功能強大,不再演示。