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)

用法:

简单折线图

x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)

p <- plot_ly(data, x = ~x, y = ~random_y, type = "scatter", mode = "lines")
p

修改折线样式

# 数据集:
month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
    "Nov", "Dec")
high_2000 <- c(32.5, 37.6, 49.9, 53, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 
    29.3)
low_2000 <- c(13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 
    15.9)
high_2007 <- c(36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76, 67.3, 46.1, 
    35)
low_2007 <- c(23.6, 14, 27, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31, 23.6)
high_2014 <- c(28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 
    39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58, 60, 58.6, 51.7, 45.2, 32.2, 
    29.1)

data <- data.frame(month, high_2000, low_2000, high_2007, low_2007, high_2014, 
    low_2014)
data$month <- factor(data$month, levels = data[["month"]])
head(data)
##   month high_2000 low_2000 high_2007 low_2007 high_2014 low_2014
## 1   Jan      32.5     13.8      36.5     23.6      28.8     12.7
## 2   Feb      37.6     22.3      26.6     14.0      28.5     14.3
## 3   Mar      49.9     32.5      43.6     27.0      37.0     18.6
## 4   Apr      53.0     37.2      52.3     36.8      56.8     35.5
## 5   May      69.1     49.9      71.5     47.6      69.7     49.9
## 6   Jun      75.4     56.1      81.4     57.7      79.7     58.0
p <- plot_ly(data, x = ~month, y = ~high_2014, name = "High 2014", type = "scatter", 
    mode = "lines", line = list(color = "rgb(205, 12, 24)", width = 4)) %>% 
    
add_trace(y = ~low_2014, name = "Low 2014", line = list(color = "rgb(22, 96, 167)", 
    width = 4)) %>% 
add_trace(y = ~high_2007, name = "High 2007", line = list(color = "rgb(205, 12, 24)", 
    width = 4, dash = "dash")) %>% 
add_trace(y = ~low_2007, name = "Low 2007", line = list(color = "rgb(22, 96, 167)", 
    width = 4, dash = "dash")) %>% 
add_trace(y = ~high_2000, name = "High 2000", line = list(color = "rgb(205, 12, 24)", 
    width = 4, dash = "dot")) %>% 
add_trace(y = ~low_2000, name = "Low 2000", line = list(color = "rgb(22, 96, 167)", 
    width = 4, dash = "dot")) %>% 
layout(title = "Average High and Low Temperatures in New York", xaxis = list(title = "Months"), 
    yaxis = list(title = "Temperature (degrees F)"))
p

将数据映射给折线类型

library(plyr)

tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = mean(len))
tg
##   supp dose length
## 1   OJ  0.5  13.23
## 2   OJ  1.0  22.70
## 3   OJ  2.0  26.06
## 4   VC  0.5   7.98
## 5   VC  1.0  16.77
## 6   VC  2.0  26.14
p <- plot_ly(tg, x = ~dose, y = ~length, type = "scatter", mode = "lines", linetype = ~supp, 
    color = I("black")) %>% 
layout(title = "The Effect of Vitamin C on Tooth Growth in Guinea Pigs by Supplement Type", 
    xaxis = list(title = "Dose in milligrams/day"), yaxis = list(title = "Tooth length"))
p

设置缺失值的连接方式:留空间断 or 跳过缺失值连接

x <- c(1:15)
y <- c(10, 20, NA, 15, 10, 5, 15, NA, 20, 10, 10, 15, 25, 20, 10)

data <- data.frame(x, y)

p <- plot_ly(data, x = ~x, y = ~y, name = "Gaps", type = "scatter", mode = "lines") %>% 
    
add_trace(y = ~y - 5, name = "<b>No</b> Gaps", connectgaps = TRUE)
p

设置折线的链接类型:

x <- c(1:5)
y <- c(1, 3, 2, 3, 1)

p <- plot_ly(x = ~x) %>% 
add_lines(y = ~y, name = "linear", line = list(shape = "linear")) %>% 
add_lines(y = y + 5, name = "spline", line = list(shape = "spline")) %>% 
add_lines(y = y + 10, name = "vhv", line = list(shape = "vhv")) %>% 
add_lines(y = y + 15, name = "hvh", line = list(shape = "hvh")) %>% 
add_lines(y = y + 20, name = "vh", line = list(shape = "vh")) %>% 
add_lines(y = y + 25, name = "hv", line = list(shape = "hv"))

p

添加文本标签或注释

x <- c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013)
y_television <- c(74, 82, 80, 74, 73, 72, 74, 70, 70, 66, 66, 69)
y_internet <- c(13, 14, 20, 24, 20, 24, 24, 40, 35, 41, 43, 50)
data <- data.frame(x, y_television, y_internet)
data
##       x y_television y_internet
## 1  2001           74         13
## 2  2002           82         14
## 3  2003           80         20
## 4  2004           74         24
## 5  2005           73         20
## 6  2006           72         24
## 7  2007           74         24
## 8  2008           70         40
## 9  2009           70         35
## 10 2010           66         41
## 11 2011           66         43
## 12 2013           69         50
# x轴相关设置:
xaxis <- list(title = "", showline = TRUE, showgrid = FALSE, showticklabels = TRUE, 
    linecolor = "rgb(204, 204, 204)", linewidth = 2, autotick = FALSE, ticks = "outside", 
    tickcolor = "rgb(204, 204, 204)", tickwidth = 2, ticklen = 5, tickfont = list(family = "Arial", 
        size = 12, color = "rgb(82, 82, 82)"))
# y轴相关设置:
yaxis <- list(title = "", showgrid = FALSE, zeroline = FALSE, showline = FALSE, 
    showticklabels = FALSE)

margin <- list(autoexpand = FALSE, l = 100, r = 100, t = 110, b = 100)

# Build the annotations
television_1 <- list(xref = "paper", yref = "y", x = 0.05, y = y_television[1], 
    xanchor = "right", yanchor = "middle", text = ~paste("Television ", y_television[1], 
        "%"), font = list(family = "Arial", size = 16, color = "rgba(67,67,67,1)"), 
    showarrow = FALSE)

internet_1 <- list(xref = "paper", yref = "y", x = 0.05, y = y_internet[1], 
    xanchor = "right", yanchor = "middle", text = ~paste("Internet ", y_internet[1], 
        "%"), font = list(family = "Arial", size = 16, color = "rgba(49,130,189, 1)"), 
    showarrow = FALSE)

television_2 <- list(xref = "paper", x = 0.95, y = y_television[12], xanchor = "left", 
    yanchor = "middle", text = paste("Television ", y_television[12], "%"), 
    font = list(family = "Arial", size = 16, color = "rgba(67,67,67,1)"), showarrow = FALSE)

internet_2 <- list(xref = "paper", x = 0.95, y = y_internet[12], xanchor = "left", 
    yanchor = "middle", text = paste("Internet ", y_internet[12], "%"), font = list(family = "Arial", 
        size = 16, color = "rgba(67,67,67,1)"), showarrow = FALSE)

p <- plot_ly(data, x = ~x) %>% 
add_trace(y = ~y_television, type = "scatter", mode = "lines", line = list(color = "rgba(67,67,67,1)", 
    width = 2)) %>% 
add_trace(y = ~y_internet, type = "scatter", mode = "lines", line = list(color = "rgba(49,130,189, 1)", 
    width = 4)) %>% 
add_trace(x = ~c(x[1], x[12]), y = ~c(y_television[1], y_television[12]), type = "scatter", 
    mode = "markers", marker = list(color = "rgba(67,67,67,1)", size = 8)) %>% 
    
add_trace(x = ~c(x[1], x[12]), y = ~c(y_internet[1], y_internet[12]), type = "scatter", 
    mode = "markers", marker = list(color = "rgba(49,130,189, 1)", size = 12)) %>% 
    
layout(title = "Main Source for News", xaxis = xaxis, yaxis = yaxis, margin = margin, 
    autosize = FALSE, showlegend = FALSE, annotations = television_1) %>% 
layout(annotations = internet_1) %>% 
layout(annotations = television_2) %>% 
layout(annotations = internet_2)
p

给折线添加置信带:

month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
    "Nov", "Dec")
high_2014 <- c(28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 
    39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58, 60, 58.6, 51.7, 45.2, 32.2, 
    29.1)
data <- data.frame(month, high_2014, low_2014)
data$average_2014 <- rowMeans(data[, c("high_2014", "low_2014")])

# The default order will be alphabetized unless specified as below:
data$month <- factor(data$month, levels = data[["month"]])
data
##    month high_2014 low_2014 average_2014
## 1    Jan      28.8     12.7        20.75
## 2    Feb      28.5     14.3        21.40
## 3    Mar      37.0     18.6        27.80
## 4    Apr      56.8     35.5        46.15
## 5    May      69.7     49.9        59.80
## 6    Jun      79.7     58.0        68.85
## 7    Jul      78.5     60.0        69.25
## 8    Aug      77.8     58.6        68.20
## 9    Sep      74.1     51.7        62.90
## 10   Oct      62.6     45.2        53.90
## 11   Nov      45.3     32.2        38.75
## 12   Dec      39.9     29.1        34.50
p <- plot_ly(data, x = ~month, y = ~high_2014, type = "scatter", mode = "lines", 
    line = list(color = "transparent"), showlegend = FALSE, name = "High 2014") %>% 
    
add_trace(y = ~low_2014, type = "scatter", mode = "lines", fill = "tonexty", 
    fillcolor = "rgba(0,100,80,0.2)", line = list(color = "transparent"), showlegend = FALSE, 
    name = "Low 2014") %>% 
add_trace(x = ~month, y = ~average_2014, type = "scatter", mode = "lines", line = list(color = "rgb(0,100,80)"), 
    name = "Average") %>% 
layout(title = "Average, High and Low Temperatures in New York", paper_bgcolor = "rgb(255,255,255)", 
    plot_bgcolor = "rgb(229,229,229)", xaxis = list(title = "Months", gridcolor = "rgb(255,255,255)", 
        showgrid = TRUE, showline = FALSE, showticklabels = TRUE, tickcolor = "rgb(127,127,127)", 
        ticks = "outside", zeroline = FALSE), yaxis = list(title = "Temperature (degrees F)", 
        gridcolor = "rgb(255,255,255)", showgrid = TRUE, showline = FALSE, showticklabels = TRUE, 
        tickcolor = "rgb(127,127,127)", ticks = "outside", zeroline = FALSE))
p

绘制密度曲线

绘图前需要提前对数据进行处理

dens <- with(diamonds, tapply(price, INDEX = cut, density))
df <- data.frame(x = unlist(lapply(dens, "[[", "x")), y = unlist(lapply(dens, 
    "[[", "y")), cut = rep(names(dens), each = length(dens[[1]]$x)))
head(df)
##                x            y  cut
## Fair1 -1114.8694 6.744087e-08 Fair
## Fair2 -1073.4981 9.022150e-08 Fair
## Fair3 -1032.1268 1.196403e-07 Fair
## Fair4  -990.7555 1.572893e-07 Fair
## Fair5  -949.3842 2.064376e-07 Fair
## Fair6  -908.0129 2.708506e-07 Fair
p <- plot_ly(df, x = ~x, y = ~y, color = ~cut) %>% add_lines()
p