R语言数据可视化包:

knitr::opts_chunk$set(echo = TRUE, eval = TRUE, tidy = TRUE, highlight = TRUE, warning = FALSE, error = FALSE, message = FALSE, include = TRUE,fig.width = 9.4,fig.height = 4)

安装包

require(devtools)
# install_github('rCharts', 'ramnathv') install_github('yihui/recharts')
# install.packages('plotly') install_github('Lchiffon/REmap')
# install.packages('igraph') install.packages('networkD3')
library(plotly)

用法:

简单条形图

p <- plot_ly(x = c("giraffes", "orangutans", "monkeys"), y = c(20, 14, 23), 
    name = "SF Zoo", type = "bar")
p

分组条形图

在layout中将barmode设置为‘group’

Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
data
##      Animals SF_Zoo LA_Zoo
## 1   giraffes     20     12
## 2 orangutans     14     18
## 3    monkeys     23     29
p <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = "bar", name = "SF Zoo") %>% 
    add_trace(y = ~LA_Zoo, name = "LA Zoo") %>% layout(yaxis = list(title = "Count"), 
    barmode = "group")
p

堆积条形图

在layout中将barmode设置为‘stack’

p <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = "bar", name = "SF Zoo") %>% 
    add_trace(y = ~LA_Zoo, name = "LA Zoo") %>% layout(yaxis = list(title = "Count"), 
    barmode = "stack")
p

修改悬浮标签

将标签列映射到text参数

x <- c("Product A", "Product B", "Product C")
y <- c(20, 14, 23)
text <- c("27% market share", "24% market share", "19% market share")
data <- data.frame(x, y, text)
data
##           x  y             text
## 1 Product A 20 27% market share
## 2 Product B 14 24% market share
## 3 Product C 23 19% market share
p0 <- plot_ly(data, x = ~x, y = ~y, type = "bar", marker = list(color = "rgb(158,202,225)", 
    line = list(color = "red", width = 1.5))) %>% 
layout(title = "January 2013 Sales Report", xaxis = list(title = ""), yaxis = list(title = ""))

p1 <- plot_ly(data, x = ~x, y = ~y, type = "bar", text = text, marker = list(color = "rgb(158,202,225)", 
    line = list(color = "rgb(8,48,107)", width = 1.5))) %>% 
layout(title = "January 2013 Sales Report", xaxis = list(title = ""), yaxis = list(title = ""))
subplot(p0, p1, nrows = 1, margin = 0.05)

将数据标签直接展示出来

在layout中设置annotations的参数text

p1 <- plot_ly(data, x = ~x, y = ~y, type = "bar", text = text, name = "p1", 
    marker = list(color = "rgb(158,202,225)", line = list(color = "rgb(8,48,107)", 
        width = 1.5))) %>% layout(title = "January 2013 Sales Report", xaxis = list(title = ""), 
    yaxis = list(title = ""), annotations = list(x = x, y = y, text = y, xanchor = "center", 
        yanchor = "bottom", showarrow = FALSE))
p1

自定义条形图的颜色

通过设置color参数,颜色值需要使用I()特别定义

x <- c("Feature A", "Feature B", "Feature C", "Feature D", "Feature E")
y <- c(20, 14, 23, 25, 22)
data <- data.frame(x, y)
data
##           x  y
## 1 Feature A 20
## 2 Feature B 14
## 3 Feature C 23
## 4 Feature D 25
## 5 Feature E 22
p0 <- plot_ly(data, x = ~x, y = ~y, type = "bar", name = "p0") %>% layout(title = "Features", 
    xaxis = list(title = ""), yaxis = list(title = ""))

p1 <- plot_ly(data, x = ~x, y = ~y, type = "bar", name = "p1", color = I("black")) %>% 
    layout(title = "Features", xaxis = list(title = ""), yaxis = list(title = ""))

subplot(p0, p1, nrows = 1, margin = 0.05)

自定义各个条形的颜色

x <- c("Feature A", "Feature B", "Feature C", "Feature D", "Feature E")
y <- c(20, 14, 23, 25, 22)
color <- c("rgba(204,204,204,1)", "rgba(222,45,38,0.8)", "rgba(204,204,204,1)", 
    "rgba(204,204,204,1)", "rgba(204,204,204,1)")
data <- data.frame(x, y, color)
data
##           x  y               color
## 1 Feature A 20 rgba(204,204,204,1)
## 2 Feature B 14 rgba(222,45,38,0.8)
## 3 Feature C 23 rgba(204,204,204,1)
## 4 Feature D 25 rgba(204,204,204,1)
## 5 Feature E 22 rgba(204,204,204,1)
p0 <- plot_ly(data, x = ~x, y = ~y, type = "bar", name = "p0", marker = list(color = color)) %>% 
    layout(title = "Least Used Features", xaxis = list(title = ""), yaxis = list(title = ""))

p1 <- plot_ly(data, x = ~x, y = ~y, type = "bar", name = "p1", marker = list(color = c("rgba(204,204,204,1)", 
    "rgba(204,204,204,1)", "rgba(204,204,204,1)", "rgba(222,45,38,0.8)", "rgba(204,204,204,1)"))) %>% 
    layout(title = "Least Used Features", xaxis = list(title = ""), yaxis = list(title = ""))

subplot(p0, p1, nrows = 1, margin = 0.05)

自定义各个条形的宽度

x = c(1, 2, 3, 5.5, 10)
y = c(10, 8, 6, 4, 2)
color <- c("rgba(204,204,204,1)", "rgba(222,45,38,0.8)", "rgba(204,204,204,1)", 
    "rgba(204,204,204,1)", "rgba(204,204,204,1)")
width = c(0.8, 0.8, 0.8, 3.5, 4)
data <- data.frame(x, y, color, width)


p <- plot_ly(data) %>% add_bars(x = ~x, y = ~y, width = ~width, marker = list(color = color))
p

映射一个颜色变量

将一个变量映射给color即可

library(dplyr)

p <- ggplot2::diamonds %>% count(cut, clarity) %>% plot_ly(x = ~cut, y = ~n, 
    color = ~clarity)
p

修改条形图的颜色和样式

x <- c(1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 
    2007, 2008, 2009, 2010, 2011, 2012)
roW <- c(219, 146, 112, 127, 124, 180, 236, 207, 236, 263, 350, 430, 474, 526, 
    488, 537, 500, 439)
China <- c(16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156, 270, 299, 340, 
    403, 549, 499)
data <- data.frame(x, roW, China)
p <- plot_ly(data, x = ~x, y = ~roW, type = "bar", name = "Rest of the World", 
    marker = list(color = "rgb(55, 83, 109)")) %>% 
add_trace(y = ~China, name = "China", marker = list(color = "rgb(26, 118, 255)")) %>% 
    
layout(title = "US Export of Plastic Scrap", xaxis = list(title = "", tickfont = list(size = 14, 
    color = "rgb(107, 107, 107)")), yaxis = list(title = "USD (millions)", titlefont = list(size = 16, 
    color = "rgb(107, 107, 107)"), tickfont = list(size = 14, color = "rgb(107, 107, 107)")), 
    legend = list(x = 0, y = 1, bgcolor = "rgba(255, 255, 255, 0)", bordercolor = "rgba(255, 255, 255, 0)"), 
    barmode = "group", bargap = 0.15, bargroupgap = 0.1)
p

绘制瀑布图

x <- c("Product<br>Revenue", "Services<br>Revenue", "Total<br>Revenue", "Fixed<br>Costs", 
    "Variable<br>Costs", "Total<br>Costs", "Total")
y <- c(400, 660, 660, 590, 400, 400, 340)
base <- c(0, 430, 0, 570, 370, 370, 0)
revenue <- c(430, 260, 690, 0, 0, 0, 0)
costs <- c(0, 0, 0, 120, 200, 320, 0)
profit <- c(0, 0, 0, 0, 0, 0, 370)
text <- c("$430K", "$260K", "$690K", "$-120K", "$-200K", "$-320K", "$370K")
data <- data.frame(x, base, revenue, costs, profit, text)

# The default order will be alphabetized unless specified as below:
data$x <- factor(data$x, levels = data[["x"]])
data
##                     x base revenue costs profit   text
## 1  Product<br>Revenue    0     430     0      0  $430K
## 2 Services<br>Revenue  430     260     0      0  $260K
## 3    Total<br>Revenue    0     690     0      0  $690K
## 4      Fixed<br>Costs  570       0   120      0 $-120K
## 5   Variable<br>Costs  370       0   200      0 $-200K
## 6      Total<br>Costs  370       0   320      0 $-320K
## 7               Total    0       0     0    370  $370K
p <- plot_ly(data, x = ~x, y = ~base, type = "bar", marker = list(color = "rgba(1,1,1, 0.0)")) %>% 
    
add_trace(y = ~revenue, marker = list(color = "rgba(55, 128, 191, 0.7)", line = list(color = "rgba(55, 128, 191, 0.7)", 
    width = 2))) %>% 
add_trace(y = ~costs, marker = list(color = "rgba(219, 64, 82, 0.7)", line = list(color = "rgba(219, 64, 82, 1.0)", 
    width = 2))) %>% 
add_trace(y = ~profit, marker = list(color = "rgba(50, 171, 96, 0.7)", line = list(color = "rgba(50, 171, 96, 1.0)", 
    width = 2))) %>% 
layout(title = "Annual Profit - 2015", xaxis = list(title = ""), yaxis = list(title = ""), 
    barmode = "stack", paper_bgcolor = "rgba(245, 246, 249, 1)", plot_bgcolor = "rgba(245, 246, 249, 1)", 
    showlegend = TRUE) %>% 
add_annotations(text = text, x = x, y = y, xref = "x", yref = "y", font = list(family = "Arial", 
    size = 14, color = "rgba(245, 246, 249, 1)"), showarrow = FALSE)
p

x <- c(1, 2, 3, 4)
y1 <- c(1, 4, 9, 16)
y2 <- c(6, -8, -4.5, 8)
y3 <- c(-15, -3, 4.5, -8)
y4 <- c(-1, 3, -3, -4)

data <- data.frame(x, y1, y2, y3, y4)

p <- plot_ly(data, x = ~x, y = ~y1, type = "bar", name = "Trace 1") %>% add_trace(y = ~y2, 
    name = "Trace 2") %>% add_trace(y = ~y3, name = "Trace 3") %>% add_trace(y = ~y4, 
    name = "Trace 4") %>% layout(title = "Relative Barmode", xaxis = list(title = "X axis"), 
    yaxis = list(title = "Y axis"), barmode = "relative")
p