Learn R | 交互可視化之Plotly包(三)
一、Filled Area Plots
1. 繪製基本的填充面積圖
# 使用ggplot2中的diamonds數據集,繪製鑽石重量的密度分布圖> density <- density(diamonds$carat)> densityCall: density.default(x = diamonds$carat)Data: diamonds$carat (53940 obs.); Bandwidth "bw" = 0.04827 x y Min. :0.0552 Min. :0.0000000 1st Qu.:1.3301 1st Qu.:0.0001509 Median :2.6050 Median :0.0037733 Mean :2.6050 Mean :0.1959014 3rd Qu.:3.8799 3rd Qu.:0.1916445 Max. :5.1548 Max. :1.7672776
> plot_ly(x = ~density$x, y = ~density$y, type = "scatter", mode = "lines", fill = "tozeroy") %>% layout(xaxis = list(title = "Carat"), yaxis = list(title = "Density"))
# 繪製cut變數在Fair與Ideal變數下的鑽石重量的密度分布圖# 數據預處理> diamonds_Fair <- filter(diamonds,cut=="Fair")> density_Fair <- density(diamonds_Fair$carat)> diamonds_Ideal <- filter(diamonds,cut=="Ideal")> density_Ideal <- density(diamonds_Ideal$carat)
> plot_ly(x = ~density_Fair$x, y = ~density_Fair$y, type = "scatter", + mode = "lines", name = "Fair cut", fill = "tozeroy") %>%+ add_trace(x = ~density_Ideal$x, y = ~density_Ideal$y, name = "Ideal cut",+ fill = "tozeroy") %>%+ layout(xaxis = list(title = "Carat"), yaxis = list(title = "Density"))
# 使用fillcolor參數自定義顏色,line定義線寬> plot_ly(x = ~density_Fair$x, y = ~density_Fair$y, type = "scatter", + mode = "lines", name = "Fair cut", fill = "tozeroy",+ fillcolor = "rgba(168, 216, 234, 0.5)",line = list(width = 0.5)) %>%+ add_trace(x = ~density_Ideal$x, y = ~density_Ideal$y, name = "Ideal cut",+ fill = "tozeroy", fillcolor = "rgba(255, 212, 96, 0.5)") %>% + layout(xaxis = list(title = "Carat"), yaxis = list(title = "Density"))
# 設定mode="none",繪製無線條的面積圖
# 基於原始數據值下的堆疊面積圖# 使用R中的USPersonalExpenditure數據集,首先進行轉置處理> uspexp <- t(USPersonalExpenditure)> uspexp <- data.frame("year"=row.names(uspexp),uspexp)> head(uspexp)> uspexp year Food.and.Tobacco Household.Operation Medical.and.Health 1940 1940 22.2 10.5 3.53 1945 1945 44.5 15.5 5.76 1950 1950 59.6 29.0 9.71 1955 1955 73.2 36.5 14.00 1960 1960 86.8 46.2 21.10 Personal.Care Private.Education1940 1.04 0.3411945 1.98 0.9741950 2.45 1.8001955 3.40 2.6001960 5.40 3.640
> plot_ly(uspexp, x = ~year, y = ~Food.and.Tobacco, + name = "Food and Tobacco", type = "scatter", mode = "none", + fill = "tozeroy", fillcolor = "#F5FF8D") %>% add_trace(y = ~Household.Operation, name = "Household Operation", fillcolor = "#50CB86") %>% add_trace(y = ~Medical.and.Health, name = "Medical and Health", fillcolor = "#4C74C9") %>% add_trace(y = ~Personal.Care, name = "Personal Care", fillcolor = "#700961") %>% add_trace(y = ~Private.Education, name = "Private Education", fillcolor = "#312F44") %>% layout(title = "United States Personal Expenditures by Categories", xaxis = list(title = "",showgrid = FALSE), yaxis = list(title = "Expenditures (in billions of dollars)",showgrid = FALSE))
# 基於比例值下的堆疊面積圖> uspexp <- t(USPersonalExpenditure)> uspexp_2 <- uspexp/rowSums(uspexp)*100 > uspexp_2 <- data.frame("year"=row.names(uspexp_2),uspexp_2)# 使用for循環,轉換為累積表> for (i in c(6:3)) {+ uspexp_2[,i-1] <- uspexp_2[,i-1] + uspexp_2[,i]+ }> uspexp_2 year Food.and.Tobacco Household.Operation Medical.and.Health 1940 1940 100 40.97471 13.05735 1945 1945 100 35.23882 12.68155 1950 1950 100 41.88768 13.61154 1955 1955 100 43.56207 15.42020 1960 1960 100 46.79416 18.47493 Personal.Care Private.Education1940 3.671798 0.90664971945 4.298978 1.41746951950 4.143916 1.75507021955 4.626060 2.00462611960 5.541253 2.2312125
> plot_ly(uspexp_2, x = ~year, y = ~Food.and.Tobacco, name = "Food and Tobacco", type = "scatter", mode = "none", fill = "tozeroy", fillcolor = "#F5FF8D") %>% add_trace(y = ~Household.Operation, name = "Household Operation", fillcolor = "#50CB86") %>% add_trace(y = ~Medical.and.Health, name = "Medical and Health", fillcolor = "#4C74C9") %>% add_trace(y = ~Personal.Care, name = "Personal Care", fillcolor = "#700961") %>% add_trace(y = ~Private.Education, name = "Private Education", fillcolor = "#312F44") %>% layout(title = "United States Personal Expenditures by Categories", xaxis = list(title = "",showgrid = FALSE), yaxis = list(title = "Proportion from the Total Expenditures", showgrid = FALSE,ticksuffix = "%"))
二、Bubble Charts
1. 簡單氣泡圖繪製
# 獲取數據> data <-read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")> head(data,3) School Women Men gap1 MIT 94 152 582 Stanford 96 151 553 Harvard 112 165 53
# 以男女數量為橫縱坐標軸,氣泡表示gap大小來繪圖> plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = "scatter", mode = "markers",marker = list(size = ~gap, opacity = 0.5, color = "rgb(255, 65, 54)")) %>% layout(title = "Gender Gap in Earnings per University", xaxis = list(showgrid = FALSE), yaxis = list(showgrid = FALSE))
# 將gap值大小同時用顏色深淺來表示,即"color = ~gap",設定系列顏色為"Reds"> plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = "scatter", + mode = "markers", color = ~gap, colors = "Reds",marker = list(size = ~gap,+ opacity = 0.5)) %>% layout(title = "Gender Gap in Earnings per University", xaxis = list(showgrid = FALSE), yaxis = list(showgrid = FALSE))
當然,我們也可以將顏色指定為非數值型的分類變數,假設我們需要這些學校所在州的gap數據的情況,可以進行以下操作:
# 首先為21個學校指定所在州> data$State <- as.factor(c("Massachusetts", "California", "Massachusetts", + "Pennsylvania", "New Jersey", "Illinois", "Washington DC","Massachusetts",+ "Connecticut", "New York", "North Carolina", "New Hampshire", "New York",+ "Indiana","New York", "Michigan", "Rhode Island", "California", "Georgia", + "California", "California"))
> plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = "scatter", + mode = "markers", size = ~gap, color = ~State, colors = "Paired",+ marker = list(opacity = 0.5, sizemode = "diameter")) %>% layout(title = "Gender Gap in Earnings per University", xaxis = list(showgrid = FALSE), yaxis = list(showgrid = FALSE),showlegend = FALSE)
# 在plot_ly()函數中,可指定sizes值的範圍修改氣泡圖的大小,例如sizes = c(10, 50)
2. 繪製自定義顯示文本的氣泡圖
> plot_ly(data, x = ~Women, y = ~Men, type = "scatter", mode = "markers", + size = ~gap, color = ~State, colors = "Paired", sizes = c(10, 50),+ marker = list(opacity = 0.5, sizemode = "diameter"), hoverinfo = "text",# <br>用於斷行+ text = ~paste("School:", School, "<br>Gender gap:", gap)) %>% layout(title = "Gender Gap in Earnings per University", xaxis = list(showgrid = FALSE), yaxis = list(showgrid = FALSE), showlegend = FALSE)
三、Pie Charts
1. 基礎餅圖的繪製
> plot_ly(data = count(diamonds, cut), labels = ~cut, values = ~n,+ name = "Cut",type = "pie") %>% layout(title = "United States Personal Expenditures by Categories in 1960", xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
2. 圓環圖
# 使用hole參數繪製圓環圖> plot_ly(data = count(diamonds, cut), labels = ~cut, values = ~n,+ name = "Cut",type = "pie",hole=0.6) %>% layout(title = "United States Personal Expenditures by Categories in 1960", xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
3. 多個面積圖的布局
# 同時查看變數cut和變數color的分布情況> plot_ly() %>%+ add_pie(data = count(diamonds, cut), labels = ~cut, values = ~n,+ name = "Cut", domain = list(x = c(0, 0.4), y = c(0, 1))) %>%+ add_pie(data = count(diamonds, color), labels = ~cut, values = ~n,+ name = "Color", domain = list(x = c(0.5, 0.9), y = c(0,1))) %>%+ layout(title = "Pie Charts with Subplots", showlegend = F,+ xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),+ yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
References:
- plotly for R
- R Graphing Library | Plotly
- Interactive Data Vizualization with R and Plotly
- A Brief Introduction to Plotly
推薦閱讀: