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)

坐标轴参数设置

axis=list(
visible = TRUE,
color = "#444",                              # 包含line, font, tick, grid的颜色
title = "axis title",                        # 坐标轴标题
titlefont = list(
                  family = "Times New Roman",# 设置字体
                  size = 1,                  # 字体大小
                  color = "black" ),         # 字体颜色
type = '-',                                  # 设置坐标轴类型,可取值: "-" , "linear" , "log" , "date" , "category"
autorange = TRUE,                            # 是否自动设置坐标轴范围,可取值 : TRUE , FALSE , "reversed"
rangemode = "normal",                        # 坐标轴范围模式 : "normal" , "tozero" , "nonnegative" 
range = list(0,20),                          # 设置坐标轴范围
fixedrange = TRUE,
tickmode = "auto",                           # 刻度线类型:"auto" | "linear" | "array"
nticks = 5,                                  # 当tickmode = "auto"时,设置刻度线的数目
tick0 = 0,                                   # 当tickmode = "linear"时,设置第一个刻度线的位置
dtick = 10,                                  # 设置刻度线的间隔,与tick0配合使用
tickvals = c(0,2,5,8,10),                    # 当tickmode = "array"时,设置刻度线的具体位置
ticktext = c('zero','two','five','eight','ten'),# 设置与tickvals对应位置的刻度线标签
ticks = "",                                  # 设置刻度线的朝向: "outside" | "inside" | ""
mirror = TRUE,                               # TRUE | "ticks" | FALSE | "all" | "allticks" ) 
ticklen = 5,                                 # 设置刻度线长度
tickwidth = 1,                               # 设置刻度线宽度
tickcolor = "#444",                          # 设置刻度线颜色

showticklabels = TRUE,                       # 是否显示刻度线标签
showspikes = TRUE,                           # 是否显示hover的点对应的水平、垂直交叉线
spikecolor = "red",                          # 交叉线的颜色
spikethickness = 1,                          # 交叉线的宽度
spikedash = "dot",                           # 交叉线的类型:"solid", "dot", "dash", "longdash", "dashdot", or "longdashdot")  ("5px,10px,2px,2px").
spikemode =  "toaxis" ,                      # 交叉线的模式:"toaxis", "across","marker", "toaxis+across", "toaxis+across+marker" 

tickfont = list(  family = "Times New Roman",# 设置刻度线标签字体
                  size = 1,                  # 字体大小
                  color = "black" ),         # 字体颜色
tickangle = 'auto',                          # 设置刻度线标签倾斜的角度
tickprefix = '',                             # 设置刻度线标签的前缀
showtickprefix = 'all',                      # 显示刻度标签前缀的模式 : "all" | "first" | "last" | "none" 
ticksuffix = "",                             # 设置刻度线标签的后缀
showticksuffix = "all",                      # 显示刻度标签后缀的模式: "all" | "first" | "last" | "none" 
showexponent = 'all',                        # 设置坐标轴标签显示的内容,"all" | "first" | "last" | "none"
exponentformat =  "B",                       # 坐标轴标签显示的格式,"none" | "e" | "E" | "power" | "SI" | "B" 
separatethousands = TRUE,                    # 设置坐标轴标签的千位符
showline = TRUE,                             # 是否绘制绘图区边框
linecolor = "#444" ,                         # 设置轴线颜色
linewidth = 1,                               # 设置轴线宽度
showgrid = TRUE,                             # 是否显示网格线
gridcolor = "#eee" ,                         # 设置网格线颜色
gridwidth = 1 ,                              # 网格线宽度
zeroline = TRUE,                             # 是否绘制x=0,y=0的线
zerolinecolor = "#444" ,                     # 零线的颜色
zerolinewidth = 1,                           # 零线的宽度
side = "top"                                 # 轴线的位置:"top" | "bottom" | "left" | "right"
)
set.seed( 123 )
x =1 : 100
y1 =2*x + rnorm( 100 )
y2 = -2*x + rnorm( 100 )

axis_x <- list( showgrid = TRUE,   # 是否显示网格线
                zeroline = TRUE,   # 是否绘制x=0,y=0的直线
                nticks = 20 ,      # 坐标轴刻度的最大数目
                showline = TRUE ,  # 是否绘制绘图区边框
                title = 'X AXIS',
                mirror = 'all',
                color = 'red')

axis_y <- list( showgrid = TRUE,   # 是否显示网格线
                zeroline = TRUE,   # 是否绘制x=0,y=0的直线
                nticks = 20 ,      # 坐标轴刻度的最大数目
                showline = FALSE , # 是否绘制绘图区边框
                title = 'Y AXIS',
                mirror = 'all',
                color = 'blue')

p1 <- plot_ly(x = x, y = y1, type = 'scatter') 
p2 <- p1 %>% layout(xaxis = axis_x)
p3 <- p1 %>% layout(yaxis = axis_y)
subplot(p1, p2, p3, nrows = 1, shareX = FALSE, shareY = FALSE)

1、设置坐标轴标题及其字体样式

axis=list(
title = "axis title",                        # 坐标轴标题
titlefont = list(
                  family = "Times New Roman",# 设置坐标轴标题字体
                  size = 20,                 # 字体大小
                  color = "red" ),           # 字体颜色
type = '-'                                   # 坐标轴类型,取值:"-", "linear", "log", "date", "category"
)

plot_ly(x = x ,y = y1 ,type = 'scatter' ) %>% layout(xaxis = axis ,yaxis = axis)

2、设置坐标轴范围

axis=list(
autorange = FALSE,                           # 是否自动设置坐标轴范围,可取值 : TRUE , FALSE , "reversed"
rangemode = "normal",                        # 坐标轴范围模式 : "normal" , "tozero" , "nonnegative"
range = c(0,4),                              # 设置坐标轴范围
fixedrange = TRUE                            # 设置坐标轴是否可以缩放
)

p1 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter')
p2 <- p1 %>% layout(xaxis = axis ,yaxis = axis)
subplot( style(p1, showlegend = FALSE), style(p2, showlegend = FALSE), nrows = 1, margin = 0.05)

3、设置坐标轴刻度线

axis=list(
autorange = FALSE,                           # 是否自动设置坐标轴范围,可取值 : TRUE , FALSE , "reversed"
rangemode = "normal",                        # 坐标轴范围模式 : "normal" , "tozero" , "nonnegative"
range = c(0,5),                              # 设置坐标轴范围
fixedrange = TRUE,                           # 设置坐标轴是否可以缩放
tickmode = "auto",                           # 刻度线类型:"auto" | "linear" | "array"
nticks = 4                                   # 当tickmode = "auto"时,设置刻度线的数目
# tick0 = 0,                                 # 当tickmode = "linear"时,设置第一个刻度线的位置
# dtick = 1,                                 # 设置刻度线的间隔,与tick0配合使用
# tickvals = c(0,0.5,1,1.5,2,5),             # 当tickmode = "array"时,设置刻度线的具体位置
# ticktext = c('zero','two','five','eight','ten'),# 设置与tickvals对应位置的刻度线标签
# ticks = "",                                  # 设置刻度线的朝向: "outside" | "inside" | ""
# mirror = TRUE,                               # TRUE | "ticks" | FALSE | "all" | "allticks" )
# ticklen = 5,                                 # 设置刻度线长度
# tickwidth = 1,                               # 设置刻度线宽度
# tickcolor = "#444",                          # 设置刻度线颜色
# showticklabels = TRUE                        # 是否显示刻度线标签
)
p1 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter')
p2 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter') %>% layout(xaxis = axis ,yaxis = axis)
subplot( style(p1, showlegend = FALSE), style(p2, showlegend = FALSE), nrows = 1, margin = 0.05)
axis=list(
autorange = FALSE,                           # 是否自动设置坐标轴范围,可取值 : TRUE , FALSE , "reversed"
rangemode = "normal",                        # 坐标轴范围模式 : "normal" , "tozero" , "nonnegative"
range = c(0,5),                              # 设置坐标轴范围
fixedrange = TRUE,                           # 设置坐标轴是否可以缩放
tickmode = "linear",                         # 刻度线类型:"auto" | "linear" | "array"
tick0 = 0,                                   # 当tickmode = "linear"时,设置第一个刻度线的位置
dtick = 0.5,                                 # 设置刻度线的间隔,与tick0配合使用
ticks = "inside",                            # 设置刻度线的朝向: "outside" | "inside" | ""
mirror = TRUE,                               # TRUE | "ticks" | FALSE | "all" | "allticks" )
ticklen = 5,                                 # 设置刻度线长度
tickwidth = 1,                               # 设置刻度线宽度
tickcolor = "#444",                          # 设置刻度线颜色
showticklabels = TRUE                        # 是否显示刻度线标签
)
p1 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter')
p2 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter') %>% layout(xaxis = axis ,yaxis = axis)
subplot( style(p1, showlegend = FALSE), style(p2, showlegend = FALSE), nrows = 1, margin = 0.05)
axis=list(
autorange = FALSE,                           # 是否自动设置坐标轴范围,可取值 : TRUE , FALSE , "reversed"
rangemode = "tozero",                        # 坐标轴范围模式 : "normal" , "tozero" , "nonnegative"
range = c(0,6),                              # 设置坐标轴范围
fixedrange = TRUE,                           # 设置坐标轴是否可以缩放
tickmode = "array",                          # 刻度线类型:"auto" | "linear" | "array"
tickvals = c(0,0.5,1,1.5,2,5),               # 当tickmode = "array"时,设置刻度线的具体位置
ticktext = c('a','b','c','d','e','e'),       # 设置与tickvals对应位置的刻度线标签
ticks = "inside",                            # 设置刻度线的朝向: "outside" | "inside" | ""
mirror = TRUE,                               # TRUE | "ticks" | FALSE | "all" | "allticks" )
ticklen = 5,                                 # 设置刻度线长度
tickwidth = 1,                               # 设置刻度线宽度
tickcolor = "red",                           # 设置刻度线颜色
showticklabels = TRUE                        # 是否显示刻度线标签
)
p1 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter')
p2 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter') %>% layout(xaxis = axis)
subplot( style(p1, showlegend = FALSE), style(p2, showlegend = FALSE), nrows = 1, margin = 0.05)
axis=list(
autorange = FALSE,                           # 是否自动设置坐标轴范围,可取值 : TRUE , FALSE , "reversed"
rangemode = "tozero",                        # 坐标轴范围模式 : "normal" , "tozero" , "nonnegative"
range = c(0,6),                              # 设置坐标轴范围
fixedrange = TRUE,                           # 设置坐标轴是否可以缩放
tickmode = "array",                          # 刻度线类型:"auto" | "linear" | "array"
tickvals = c(0,0.5,1,1.5,2,5),               # 当tickmode = "array"时,设置刻度线的具体位置
ticks = "inside",                            # 设置刻度线的朝向: "outside" | "inside" | ""
ticklen = 5,                                 # 设置刻度线长度
tickwidth = 1,                               # 设置刻度线宽度
tickcolor = "red",                           # 设置刻度线颜色

showticklabels = TRUE,                       # 是否显示刻度线标签
tickprefix = '价格:',                        # 设置刻度线标签的前缀
showtickprefix = 'first',                    # 显示刻度标签前缀的模式 : "all" | "first" | "last" | "none"
ticksuffix = "元",                           # 设置刻度线标签的后缀
showticksuffix = "last",                     # 显示刻度标签后缀的模式: "all" | "first" | "last" | "none"
# showexponent = 'first',                    # 设置坐标轴标签显示的指数内容,"all" | "first" | "last" | "none"
# exponentformat =  "B",                     # 坐标轴标签显示的格式,"none" | "e" | "E" | "power" | "SI" | "B"
separatethousands = TRUE                     # 设置坐标轴标签的千位符
)
p1 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter')
p2 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter') %>% layout(xaxis = axis)
subplot( style(p1, showlegend = FALSE), style(p2, showlegend = FALSE), nrows = 1, margin = 0.05)

设置刻度线标签的前缀和后缀

axis=list(
showticklabels = TRUE,                       # 是否显示刻度线标签
tickprefix = '价格:',                        # 设置刻度线标签的前缀
showtickprefix = 'all',
ticksuffix = "元",                           # 设置刻度线标签的后缀
showticksuffix = "all"                       # 显示刻度标签后缀的模式: "all" | "first" | "last" | "none"
)
p1 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter')
p2 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter') %>% layout(xaxis = axis, yaxis = axis)
subplot( style(p1, showlegend = FALSE), style(p2, showlegend = FALSE), nrows = 1, margin = 0.05)

设置hover的水平、垂直交叉线的样式

axis=list(
showspikes = TRUE,                           # 是否显示hover的点对应的水平、垂直交叉线
spikecolor = "red",                          # 交叉线的颜色
spikethickness = 1,                          # 交叉线的宽度
spikedash = "dot",                           # 交叉线的类型:"solid", "dot", "dash", "longdash", "dashdot", or "longdashdot")  ("5px,10px,2px,2px").
spikemode =  "across"                        # 交叉线的模式:"toaxis", "across","marker", "toaxis+across", "toaxis+across+marker"
)
p1 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter')
p2 <- plot_ly(x = 1:5, y = 1:5, type = 'scatter') %>% layout(xaxis = axis, yaxis = axis)
subplot( style(p1, showlegend = FALSE), style(p2, showlegend = FALSE), nrows = 1, margin = 0.05)

设置网格线样式

axis=list(
showline = TRUE,                             # 是否绘制绘图区边框
linecolor = "#444" ,                         # 设置轴线颜色
linewidth = 1,                               # 设置轴线宽度
showgrid = TRUE,                             # 是否显示网格线
gridcolor = "#eee" ,                         # 设置网格线颜色
gridwidth = 1 ,                              # 网格线宽度
zeroline = FALSE,                            # 是否绘制x=0,y=0的线
zerolinecolor = "#444" ,                     # 零线的颜色
zerolinewidth = 1,                           # 零线的宽度
side = "bottom"                              # 轴线的位置:"top" | "bottom" | "left" | "right"
)

p1 <- plot_ly(x = -2:5, y = -2:5, type = 'scatter')
p2 <- p1 %>% layout(xaxis = axis, yaxis = axis, showlegend = FALSE)
p3 <- p1 %>% layout(xaxis = list(showline = TRUE, showgrid = FALSE, zeroline = FALSE), yaxis = list(showline = TRUE, showgrid = FALSE, zeroline = FALSE), showlegend = FALSE)
p4 <- p1 %>% layout(xaxis = list(showline = FALSE, showgrid = TRUE,gridcolor = 'red', gridwidth = 2, zeroline = FALSE), showlegend = FALSE)
subplot( style(p1, showlegend = FALSE), style(p2, showlegend = FALSE),p3,p4, nrows = 2, margin = 0.05)