介紹除了 ggplot 外的繪圖套件,當想要呈現出來的資訊是可以讓使用者互動點選使用的套件可以用什麼;以及介紹其他繪圖工具。

leaflet

  • 主要是用來畫互動式地圖的套件
  • 比較友善的是標所在地
  • 比較麻煩的是填顏色,但相對可調控度高
    • colorNumeric()
    • colorQuantile()

語法架構

leaflet(data = shp) %>% addTiles() %>%
addMarkers(lng=, lat=, popup=“顯現的文字”) %>% addPolygons(fillColor = ) %>% addProviderTiles() %>% addLegend()


cor <- colorNumeric(
  palette = c("green","red"),
  domain = tic@data$number)

leaflet(data = tic) %>% 
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(color = "#444444",weight = 1, smoothFactor = 0.5, 
              opacity = 1, fillColor = ~cor(number)) %>% 
  addLegend("bottomright", pal = cor, values = ~ number,
    title = " Random",
    labFormat = labelFormat(prefix = "$"),
    opacity = 0.8)
#######
cus$lng <- cus$lng + runif(dim(cus)[1], -0.008,0.008)
getColor <- function(quakes) {
  sapply(cus$X21.失能程度, function(mag) {
    if(mag == "重度") {
      "#de2d26"
    } else if(mag == "中度") {
      "#fc9272"
    } else {
      "#fee0d2"
    } })
}

pou <- paste0("編號:", cus$編號,"<br>失能程度:", cus$X21.失能程度)
leaflet(data = tic) %>% 
  setView(lng = mean(tic@bbox[1,]), lat = mean(tic@bbox[2,]), zoom = 12) %>% 
  addPolygons(color="gray", weight = 1.5, smoothFactor = 0.5,   opacity = 0.8) %>% 
  addCircleMarkers(lng=cus$lng, lat=cus$lat, 
                   radius = 3,color=getColor(cus),popup=pou) %>% 
   addTiles()


plotly

  • 互動式圖表的套件
  • 一般的敘述性統計圖表
  • 地圖式呈現圖表

語法架構

data %>% ploy_ly( x = ~x, y = ~y, frame = ~f, size = ~s, text = ~t, color = ~c, type = “scatter” “bar” “histogram” “heatmap” “box”, mode = “markers” “line” )

data %>% ploy_ly() %>% add_histogram() add_box_plot()


用 R 內 diamonds dataset 做demo
## # A tibble: 6 x 10
##   carat       cut color clarity depth table price     x     y     z
##   <dbl>     <ord> <ord>   <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23     Ideal     E     SI2  61.5    55   326  3.95  3.98  2.43
## 2  0.21   Premium     E     SI1  59.8    61   326  3.89  3.84  2.31
## 3  0.23      Good     E     VS1  56.9    65   327  4.05  4.07  2.31
## 4  0.29   Premium     I     VS2  62.4    58   334  4.20  4.23  2.63
## 5  0.31      Good     J     SI2  63.3    58   335  4.34  4.35  2.75
## 6  0.24 Very Good     J    VVS2  62.8    57   336  3.94  3.96  2.48
plot_ly(diamonds, x = ~cut, type = "histogram") 
plot_ly(diamonds, x = ~price, y = ~interaction(clarity, cut)) %>%
  add_boxplot(color = ~clarity) %>%
  layout(yaxis = list(title = ""), margin = list(l = 100))
diamonds %>%  plot_ly (
  x = ~carat ,
  y = ~price ,
  color = ~color,
  type = 'scatter',
  mode = 'markers')
diamonds %>%  plot_ly(
    x = ~carat ,
    y = ~price ,
    frame = ~cut,
    type = 'scatter',
    mode = 'markers'
  )