最近看到很多利用 HTML5, d3.js, SVG 來製作動態網頁內容的 R packages, 現在我們用短短幾行指令,就可以在網頁裡面置入很棒的互動式動態圖表了。


# Loading libraries
library(sunburstR)
library(highcharter)
library(googleVis)
library(DT)
library(rpivotTable)
library(rAmCharts)

sunburstR

sequences <- read.csv(
  system.file("examples/visit-sequences.csv",package="sunburstR")
  ,header=F
  ,stringsAsFactors = FALSE
)
sunburst(sequences)
Legend

highcharter

data(diamonds, mpg, package = "ggplot2")
hchart(mpg, "scatter", hcaes(x = displ, y = hwy, group = class))
highchart() %>% 
  hc_chart(type = "line") %>% 
  hc_title(text = "Monthly Average Temperature") %>% 
  hc_subtitle(text = "Source: WorldClimate.com") %>% 
  hc_xAxis(categories = c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) %>% 
  hc_yAxis(title = list(text = "Temperature (C)")) %>% 
  hc_plotOptions(line = list(
    dataLabels = list(enabled = TRUE),
    enableMouseTracking = FALSE) ) %>% 
  hc_series(
    list(name = "Tokyo",
         data = c(7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6) ),
    list(name = "London",
         data = c(3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8) ) )
hc <- highcharts_demo()
hc
hc %>% hc_add_theme(hc_theme_flat())
hc %>% hc_add_theme(hc_theme_sandsignika())
hc %>% hc_add_theme(hc_theme_handdrawn())


hchart(princomp(USArrests, cor = TRUE))


googleVis

Here is a little pie:

M <- gvisPieChart(CityPopularity,options=list(width=800,height=400))
plot(M, 'chart')

Here is a little motion chart example:

M <- gvisMotionChart(Fruits, "Fruit", "Year",
                     options=list(width=800, height=600))
plot(M, 'chart')


DT

Here is a DT example:

datatable(iris)

Here is a DT example:

datatable(mtcars, #filter = 'top',
  options = list(pageLength = 8, autoWidth = TRUE))


rAmCharts

data("data_pie")
amPie(data_pie, inner_radius=50, depth=10, show_values=TRUE, legend=TRUE)
data(data_gbar)
pipeR::pipeline(
  amBarplot(x = "year", y = c("income", "expenses"), data = data_gbar,
            dataDateFormat = "YYYY", minPeriod = "YYYY"),
  setChartCursor()
)
d <- data.frame(get(x = "USArrests", pos = "package:datasets"))
d$state = rownames(d)
amBarplot(x="state", y=c("Murder", "Assault", "UrbanPop", "Rape"),
          data=d, horiz = F,
          legend=T, legendPos='bottom', zoom=T,
          stack_type = "regular")
data("data_wind")
amWind(data = data_wind)
data <- data.frame(label = c("A", "Z", "E", "R", "T"),
                   Product1 = c(1, 2, 3, 4, 2),
                   Product2 = c(2, 8, 1, 1, 0),
                   Product3 = c(1,1,2,2,4))
amRadar(data, legend=T, main="My Radar")


rpivotTable

rpivotTable(mtcars, rows="gear", col="cyl", aggregatorName="Average",
            vals="mpg", rendererName="Table",
            height=600,width=800)