数据可视化完美指南-R-Python

从数据到图表

faae614c-bfc6-40a0-b812-286cbe9f43d8.png

有什么样的数据做什么样的图

作者提供了一张树状图,帮助并引导我们找到合适自己数据的的可视化方式 What kind of data do you have? Pick the main type using the buttons below. Then let the decision tree guide you toward your graphic possibilities.

着便是有名的: https://www.data-to-viz.com/ 网站。

Yan Holtz 和 Conor Healys 两个人关系很好,一起在业余时间开发了这个网站。基于 R 和 Python 做的源代码,这里我们不仅可以得到大量优秀的源代码,同时我们可以得到一张决策树,用于知道如何使用代码。这两个人相当厉害了,不仅仅给大家了工具,还叫大家如何使用。作为无私的分享,如果对大家有用,请在文章中致谢他们。如果我们需要交流代码,和谁交流呢?那必须是 Yan Holtz,这位主要负责代码部分。Conor Healys 负责图形设计工作。

1bf700c6-92ed-476e-ba53-bf26d6b13bd5.png

可视化架构

b6ec70fc-9159-4d8c-9f3d-4a62ea85d8c2.png

基于网站我们来做一个示例

大部分情况,我们的数据都是二维数据框:下面就二维数据框的数据,变量指定为有顺序的变量,我们进行出图:

基于有顺序的二维数据框的出图

这是基于时间序列的一份二维数据。作者提供了数据下载地址。 as.Date 函数将数据转化为时间序列。

# Libraries
library(tidyverse)
## -- Attaching packages ----------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.2.0     √ purrr   0.3.2
## √ tibble 2.1.3 √ dplyr 0.8.3
## √ tidyr 0.8.3 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.4.0
## -- Conflicts -------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(patchwork)
# install.packages("babynames")
library(babynames)
library(viridis)
## Loading required package: viridisLite
# ?as.Date
# Load dataset from github
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv", header=T)
data$date <- as.Date(data$date)

这里仅仅提取最后的十个数据进行点线图的可视化

# Plot
data %>%
tail(10) %>%
ggplot( aes(x=date, y=value)) +
geom_line(color="#69b3a2") +
geom_point(color="#69b3a2", size=4) +
ggtitle("Evolution of Bitcoin price") +
ylab("bitcoin price ($)") +
theme_ipsum()

27741a94-20ec-4638-90c8-75d9b256c247.png

这里使用最后的 60 个数据进行可视化

这里做了折线图和点线图。我们 ggplot 出图就是这么随意,图形相加就是拼图。

# Plot
p1 <- data %>%
tail(60) %>%
ggplot( aes(x=date, y=value)) +
geom_line(color="#69b3a2") +
ggtitle("Line chart") +
ylab("bitcoin price ($)") +
theme_ipsum()

p2 <- data %>%
tail(60) %>%
ggplot( aes(x=date, y=value)) +
geom_line(color="#69b3a2") +
geom_point(color="#69b3a2", size=2) +
ggtitle("Connected scatterplot") +
ylab("bitcoin price ($)") +
theme_ipsum()

p = p1 + p2
p

bd01a70b-3b63-47b3-a82b-43f8047e097c.png

散点图展示时间序列

# Plot
data %>%
tail(60) %>%
ggplot( aes(x=date, y=value)) +
geom_point(color="#69b3a2", size=2) +
ggtitle("Line chart") +
ylab("bitcoin price ($)") +
theme_ipsum()

2d175cd2-3ff7-427e-9a65-72f8389bc9bb.png

分组时间序列可视化

library(babynames)

# Load dataset
data <- babynames %>%
filter(name %in% c("Ashley", "Amanda")) %>%
filter(sex=="F")

#plot
data %>%
ggplot( aes(x=year, y=n, group=name, color=name)) +
geom_line() +
scale_color_viridis(discrete = TRUE, name="") +
theme(legend.position="none") +
ggtitle("Popularity of American names in the previous 30 years") +
theme_ipsum()

ce3f8e74-66d1-44d8-8a62-a33b8360adae.png

geom_segment 函数突出展示变化趋势

library(grid) # needed for arrow function
library(ggrepel)

# data
tmp <- data %>%
filter(year>1970) %>%
select(year, name, n) %>%
spread(key = name, value=n, -1)

# data for date
tmp_date <- tmp %>% sample_frac(0.3)

tmp%>%
ggplot(aes(x=Amanda, y=Ashley, label=year)) +
geom_point(color="#69b3a2") +
geom_text_repel(data=tmp_date) +
geom_segment(color="#69b3a2",
aes(
xend=c(tail(Amanda, n=-1), NA),
yend=c(tail(Ashley, n=-1), NA)
),
arrow=arrow(length=unit(0.3,"cm"))
) +
theme_ipsum()

ab1ab941-e7ca-4e21-a3da-f3a0a1b70252.png

data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv", header=T)
data$date <- as.Date(data$date)

p1 <- data %>%
tail(10) %>%
ggplot( aes(x=date, y=value)) +
geom_line(color="#69b3a2") +
geom_point(color="#69b3a2", size=4) +
ggtitle("Not cuting") +
ylab("bitcoin price ($)") +
theme_ipsum() +
ylim(0,10000)

p2 <- data %>%
tail(10) %>%
ggplot( aes(x=date, y=value)) +
geom_line(color="#69b3a2") +
geom_point(color="#69b3a2", size=4) +
ggtitle("Cuting") +
ylab("bitcoin price ($)") +
theme_ipsum()

p1 + p2

52566d0b-6a22-4631-8e0b-6cd14fd269e2.png


参考资料

  • https://www.data-to-viz.com/graph/connectedscatter.html



往期精彩:

c885a523-e59c-46bf-85f3-6fd5084113fc.png

本文分享自微信公众号 - 生信科技爱好者(bioitee)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“ OSC源创计划 ”,欢迎正在阅读的你也加入,一起分享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值