常用 7 大类型图形可视化——变化趋势图形

简介: 常用 7 大类型图形可视化——变化趋势图形

引言

在进行数据分析时,免不了对结果进行可视化。那么,什么样的图形才最适合自己的数据呢?一个有效的图形应具备以下特点:

  • 能正确传递信息,而不会产生歧义;
  • 样式简单,但是易于理解;
  • 添加的图形美学应辅助理解信息;
  • 图形上不应出现冗余无用的信息。

本系列推文,小编将汇总可视化中常用 7 大类型图形,供读者参考。

常用 7 大类型图形可视化——组成成分图形

常用 7 大类型图形可视化——分布

常用 7 大类型图形可视化——排序关系图形

常用 7 大类型图形可视化——偏差关系图形

常用 7 大类型图形可视化——相关关系图形

每类制作成一篇推文,主要参考资料为:Top 50 ggplot2 Visualizations[1]。其他类似功能网站,资料包括:

  1. 庄闪闪的可视化笔记——常用图形[2]
  2. R Graph Gallery[3]
  3. 《R 语言教程》——ggplot 的各种图形[4]

系列目录


本文主要介绍第六部分:变化趋势图形。

加载数据集

使用 ggplot2 包中自带数据集作为示例数据集。

library(ggplot2)
library(plotrix)
data("midwest", package = "ggplot2") #加载数据集


midwest 数据集

全局主题设置

全局配色、主题设置。注意,本文使用离散色阶,如果需要使用连续色阶,则需要重写。

options(scipen=999)  # 关掉像 1e+48 这样的科学符号
# 颜色设置(灰色系列)
cbp1 <- c("#999999", "#E69F00", "#56B4E9", "#009E73",
          "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# 颜色设置(黑色系列)
cbp2 <- c("#000000", "#E69F00", "#56B4E9", "#009E73",
          "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
ggplot <- function(...) ggplot2::ggplot(...) + 
  scale_color_manual(values = cbp1) +
  scale_fill_manual(values = cbp1) + # 注意: 使用连续色阶时需要重写
  theme_bw()


6 变化趋势

6.1 时间序列图:基于时间序列对象(ts)

ggfortify 包中的 autoplot() 可以对时间序列直接绘图。在此基础上,添加其他 ggplot 相关函数。

library(ggplot2)
library(ggfortify)
theme_set(theme_classic())
# 绘图 
autoplot(AirPassengers) + 
  labs(title="AirPassengers") + 
  theme(plot.title = element_text(hjust=0.5))


时间序列图:基于时间序列对象(ts)

6.2 时间序列图:基于数据框

library(ggplot2)
theme_set(theme_classic())
# 使用默认的时间跨度
ggplot(economics, aes(x=date)) + 
  geom_line(aes(y=pce)) + 
  labs(title="Time Series Chart", 
       caption="Source: Economics")


economics 数据集预览:来自 ggplot2 包


时间序列图:基于数据框

如果想设置特定的时间间隔,则需要使用 scale_x_date() 函数。

library(ggplot2)
library(lubridate)
theme_set(theme_bw())
economics_m <- economics[1:24, ]
# 设定时间跨度为一个月
lbls <- paste0(month.abb[month(economics_m$date)], " ", lubridate::year(economics_m$date))
brks <- economics_m$date
# 绘图
ggplot(economics_m, aes(x=date)) + 
  geom_line(aes(y=pce)) + 
  scale_x_date(labels = lbls, 
               breaks = brks) +  # change to monthly ticks and labels
  theme(axis.text.x = element_text(angle = 90, vjust=0.5),  # rotate x axis text
        panel.grid.minor = element_blank())  # turn off minor grid


时间序列图:跨度为一个月

设置时间跨度为 1 年:

library(ggplot2)
library(lubridate)
theme_set(theme_bw())
economics_y <- economics[1:90, ] # 选择一年数据
# X轴文本的标签和 break 值
brks <- economics_y$date[seq(1, length(economics_y$date), 12)]
lbls <- lubridate::year(brks)
# 绘图
ggplot(economics_y, aes(x=date)) + 
  geom_line(aes(y=pce)) + 
  scale_x_date(labels = lbls, 
               breaks = brks) +  # change to monthly ticks and labels
  theme(axis.text.x = element_text(angle = 90, vjust=0.5),  # rotate x axis text
        panel.grid.minor = element_blank())


时间序列图:时间跨度为 1 年

6.3 多个时间序列

在本例中,基于长数据格式进行可视化。这意味着,所有列的列名和各自的值被存放在两个变量中(分别是 variable 和 value)。

data(economics_long, package = "ggplot2")
head(economics_long)



在下面的代码中,在 geom_line() 函数中设置绘图对象为 value,颜色匹配对象为 variable。这样,只要调用一次 geom_line,就会绘制多条彩色线,每条线代表 variable 列中的每个唯一 value 。scale_x_date() 将更改 X 轴断点和标签,scale_color_manual 将更改行颜色。

library(ggplot2)
library(lubridate)
theme_set(theme_bw())
df <- economics_long[economics_long$variable %in% c("psavert", "uempmed"), ]
df <- df[lubridate::year(df$date) %in% c(1967:1981), ]
# labels and breaks for X axis text
brks <- df$date[seq(1, length(df$date), 12)]
lbls <- lubridate::year(brks)
# 绘图
ggplot(df, aes(x=date)) + 
  geom_line(aes(y=value, col=variable)) + 
  labs(title="Time Series of Returns Percentage", 
       subtitle="Drawn from Long Data format", 
       caption="Source: Economics", 
       y="Returns %", 
       color=NULL) +  # title and caption
  scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
  scale_color_manual(labels = c("psavert", "uempmed"), 
                     values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # line color
  theme(axis.text.x = element_text(angle = 90, vjust=0.5, size = 8),  # rotate x axis text
        panel.grid.minor = element_blank())  # turn off minor grid


多个时间序列

如果从一个宽格式创建一个时间序列,则必须通过对每条线调用一次 geom_line() 。因此,默认情况下不会绘制图例,需要手动添加。

library(ggplot2)
library(lubridate)
theme_set(theme_bw())
df <- economics[, c("date", "psavert", "uempmed")]
df <- df[lubridate::year(df$date) %in% c(1967:1981), ]
# labels and breaks for X axis text
brks <- df$date[seq(1, length(df$date), 12)]
lbls <- lubridate::year(brks)
# plot
ggplot(df, aes(x=date)) + 
  geom_line(aes(y=psavert, col="psavert")) + 
  geom_line(aes(y=uempmed, col="uempmed")) + 
  labs(title="Time Series of Returns Percentage", 
       subtitle="Drawn From Wide Data format", 
       caption="Source: Economics", y="Returns %") +  # title and caption
  scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
  scale_color_manual(name="", 
                     values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # line color
  theme(panel.grid.minor = element_blank())  # turn off minor grid


多个时间序列

6.4 堆叠面积图

堆叠面积图与折线图类似,只是图下方的区域全部着色。应用场景有:

  • 想要描述数量或体积(而不是价格之类的变量)随时间的变化;
  • 有很多数据点。对于很少的数据点,可以考虑绘制柱状图。
  • 希望展示各个类别的贡献。
library(ggplot2)
library(lubridate)
theme_set(theme_bw())
df <- economics[, c("date", "psavert", "uempmed")]
df <- df[lubridate::year(df$date) %in% c(1967:1981), ]
# labels and breaks for X axis text
brks <- df$date[seq(1, length(df$date), 12)]
lbls <- lubridate::year(brks)
# plot
ggplot(df, aes(x=date)) + 
  geom_area(aes(y=psavert+uempmed, fill="psavert")) + 
  geom_area(aes(y=uempmed, fill="uempmed")) + 
  labs(title="Area Chart of Returns Percentage", 
       subtitle="From Wide Data format", 
       caption="Source: Economics", 
       y="Returns %") +  # title and caption
  scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
  scale_fill_manual(name="", 
                    values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # line color
  theme(panel.grid.minor = element_blank())  # turn off minor grid


堆叠面积图

6.5 日历热力图

当您想要在实际的日历上看到像股票价格这类指标的变化,特别是高点和低点时,日历热力图是一个很好的工具。它强调随着时间的推移视觉上的变化,而不是实际数值的变化。这可以通过使用 geom_tile() 来实现。

拓展:庄小编以前介绍过如何绘制日历图,可参见:calendR包—私人定制专属日历;私人定制日历代码改进。

library(ggplot2)
library(plyr)
library(scales)
library(zoo)
df <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/yahoo.csv")
df$date <- as.Date(df$date)  # 格式化日期
df <- df[df$year >= 2012, ]  # filter reqd years
# 创建月周
df$yearmonth <- as.yearmon(df$date)
df$yearmonthf <- factor(df$yearmonth)
df <- ddply(df,.(yearmonthf), transform, monthweek=1+week-min(week))  # compute week number of month
df <- df[, c("year", "yearmonthf", "monthf", "week", "monthweek", "weekdayf", "VIX.Close")]
head(df)



ggplot(df, aes(monthweek, weekdayf, fill = VIX.Close)) + 
  geom_tile(colour = "white") + 
  facet_grid(year~monthf) + 
  scale_fill_gradient(low="red", high="green") +
  labs(x="Week of Month",
       y="",
       title = "Time-Series Calendar Heatmap", 
       subtitle="Yahoo Closing Price", 
       fill="Close")



日历热力图

6.6 坡度图

坡度图可以可视化数值和类别排名之间的变化。这更适用于时间点很少的时间序列。下面给出使用 ggplot2 包绘制的案例,来源于:Top 50 ggplot2 Visualizations[5]。内部代码,这里不做过多解释,有能力的读者请自行研究!

此外,关于坡度图的绘制,也有些大佬已经集成 R 包了,例如:CGPfunctions[6] 包中的 newggslopegraph() 、slopegraph [7] 包中的 ggslopegraph() 绘制等。

library(dplyr)
theme_set(theme_classic())
source_df <- read.csv("https://raw.githubusercontent.com/jkeirstead/r-slopegraph/master/cancer_survival_rates.csv")
# 定义函数,来源: https://github.com/jkeirstead/r-slopegraph
tufte_sort <- function(df, x="year", y="value", group="group", method="tufte", min.space=0.05) {
    ## First rename the columns for consistency
    ids <- match(c(x, y, group), names(df))
    df <- df[,ids]
    names(df) <- c("x", "y", "group")
    ## Expand grid to ensure every combination has a defined value
    tmp <- expand.grid(x=unique(df$x), group=unique(df$group))
    tmp <- merge(df, tmp, all.y=TRUE)
    df <- mutate(tmp, y=ifelse(is.na(y), 0, y))
    ## Cast into a matrix shape and arrange by first column
    require(reshape2)
    tmp <- dcast(df, group ~ x, value.var="y")
    ord <- order(tmp[,2])
    tmp <- tmp[ord,]
    min.space <- min.space*diff(range(tmp[,-1]))
    yshift <- numeric(nrow(tmp))
    ## Start at "bottom" row
    ## Repeat for rest of the rows until you hit the top
    for (i in 2:nrow(tmp)) {
        ## Shift subsequent row up by equal space so gap between
        ## two entries is >= minimum
        mat <- as.matrix(tmp[(i-1):i, -1])
        d.min <- min(diff(mat))
        yshift[i] <- ifelse(d.min < min.space, min.space - d.min, 0)
    }
    tmp <- cbind(tmp, yshift=cumsum(yshift))
    scale <- 1
    tmp <- melt(tmp, id=c("group", "yshift"), variable.name="x", value.name="y")
    ## Store these gaps in a separate variable so that they can be scaled ypos = a*yshift + y
    tmp <- transform(tmp, ypos=y + scale*yshift)
    return(tmp)
}
plot_slopegraph <- function(df) {
    ylabs <- subset(df, x==head(x,1))$group
    yvals <- subset(df, x==head(x,1))$ypos
    fontSize <- 3
    gg <- ggplot(df,aes(x=x,y=ypos)) +
        geom_line(aes(group=group),colour="grey80") +
        geom_point(colour="white",size=8) +
        geom_text(aes(label=y), size=fontSize, family="American Typewriter") +
        scale_y_continuous(name="", breaks=yvals, labels=ylabs)
    return(gg)
}    
## 准备数据 
df <- tufte_sort(source_df, 
                 x="year", 
                 y="value", 
                 group="group", 
                 method="tufte", 
                 min.space=0.05)
df <- transform(df, 
                x=factor(x, levels=c(5,10,15,20), 
                            labels=c("5 years","10 years","15 years","20 years")), 
                y=round(y))
## 绘图
plot_slopegraph(df) + labs(title="Estimates of % survival rates") + 
                      theme(axis.title=element_blank(),
                            axis.ticks = element_blank(),
                            plot.title = element_text(hjust=0.5,
                                                      family = "American Typewriter",
                                                      face="bold"),
                            axis.text = element_text(family = "American Typewriter",
                                                     face="bold"))


坡度图

6.7 季节图

如果您正在处理 ts 或 xts 类型的时间序列对象,您可以通过使用 forecast::ggseasonplot() 绘制的季节图来查看季节波动。下面是一个使用 AirPassengers 和 nottem 数据集绘制的例子。

library(ggplot2)
library(forecast)
theme_set(theme_classic())
# 使用子集数据
nottem_small <- window(nottem, start=c(1920, 1), end=c(1925, 12))  # 使用较小时间窗的子集


nottem 数据集

ggseasonplot(nottem_small) + 
  labs(title="Seasonal plot: Air temperatures at Nottingham Castle")


nottem 数据集的季节图


AirPassengers 数据集

# 绘图
ggseasonplot(AirPassengers) + 
  labs(title="Seasonal plot: International Airline Passengers")


AirPassengers 数据集的季节图

参考资料

[1]

Top 50 ggplot2 Visualizations: http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html

[2]

庄闪闪的可视化笔记——常用图形: https://liangliangzhuang.github.io/R-tutorial/main-diagram-types.html

[3]

R Graph Gallery: https://www.r-graph-gallery.com/ggplot2-package.html

[4]

R 语言教程——ggplot 的各种图形: https://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/ggplotvis.html

[5]

Top 50 ggplot2 Visualizations: http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html

[6]

CGPfunctions: https://cran.r-project.org/web/packages/CGPfunctions/vignettes/Using-newggslopegraph.html

[7]

slopegraph : https://rdrr.io/github/leeper/slopegraph/

目录
相关文章
|
程序员 Windows
markdown的基本语法
markdown的基本语法
554 0
|
9月前
|
人工智能 开发工具 C++
LangGraph vs Semantic Kernel:状态图与内核插件的两条技术路线对比
本文对比2026年最新版LangGraph(v1.0)与Semantic Kernel(v1.28.1),澄清过时认知:LangGraph已成为LangChain执行引擎,支持持久化状态机;SK则原生集成MCP协议,定位AI中间件。二者架构迥异——图编排vs插件组合,运行时托管状态vs开发者自主管理。附代码实操与选型指南。
586 2
LangGraph vs Semantic Kernel:状态图与内核插件的两条技术路线对比
|
存储 人工智能 自然语言处理
RAL-Writer Agent:基于检索与复述机制,让长文创作不再丢失关键信息
RAL-Writer Agent是一种专业的人工智能写作辅助技术,旨在解决生成高质量、内容丰富的长篇文章时所面临的技术挑战,确保全文保持连贯性和相关性。本研究将系统分析RAL-Writer的核心技术架构、功能特点及其在内容创作、学术研究和专业交流领域的应用前景。
505 4
RAL-Writer Agent:基于检索与复述机制,让长文创作不再丢失关键信息
|
人工智能 自然语言处理 人机交互
数字人|数字人平台竞争企业技术分析
数字人企业正以技术革新重塑产业生态。像衍科技、追光数字、元界智能领跑赛道,分别在底层技术、商业应用与生态构建上突破。从虚拟客服到元宇宙入口,数字人迈向情感交互新阶段,千亿市场加速成型,开启虚拟与现实融合的“身份革命”。
|
JavaScript 前端开发 开发者
"震撼揭秘!ElementUI Table组件大变身,一键自定义升序降序按钮,排序操作秒变炫酷舞台,点击即燃,你的列表排序从此不再平凡!"
【8月更文挑战第16天】在Vue.js开发中,ElementUI的Table组件因其实用性和灵活性深受喜爱。本文通过一个后台管理系统用户列表的案例,展示了如何自定义添加“升序”和“降序”按钮以增强排序功能。首先确保安装Vue与ElementUI;接着定义表格数据及初始排序状态;利用scoped slot自定义列模板,加入按钮并绑定点击事件;最后实现在点击按钮时更新排序状态并重新排序数据的逻辑。这种方法不仅改善了用户体验,还为开发者提供了灵活调整排序逻辑的空间。
920 3
|
机器学习/深度学习 存储 数据中心
《深度揭秘:TPU张量计算架构如何重塑深度学习运算》
TPU(张量处理单元)是谷歌为应对深度学习模型计算需求而设计的专用硬件。其核心矩阵乘法单元(MXU)采用脉动阵列架构,显著提升矩阵运算效率;内存管理单元优化数据流通,减少瓶颈;控制单元协调系统运作,确保高效稳定。TPU在训练和推理速度、能耗方面表现出色,大幅缩短BERT等模型的训练时间,降低数据中心成本。尽管通用性和易用性仍有挑战,但TPU已为深度学习带来革命性变化,未来有望进一步优化。
1440 19
|
存储 消息中间件 NoSQL
Redis数据结构:List类型全面解析
Redis数据结构——List类型全面解析:存储多个有序的字符串,列表中每个字符串成为元素 Eelement,最多可以存储 2^32-1 个元素。可对列表两端插入(push)和弹出(pop)、获取指定范围的元素列表等,常见命令。 底层数据结构:3.2版本之前,底层采用**压缩链表ZipList**和**双向链表LinkedList**;3.2版本之后,底层数据结构为**快速链表QuickList** 列表是一种比较灵活的数据结构,可以充当栈、队列、阻塞队列,在实际开发中有很多应用场景。
1681 5
|
人工智能 计算机视觉 C++
AI计算机视觉笔记七:基于mediapipe的虚拟鼠标控制
该项目旨在通过摄像头识别手指动作以实现鼠标控制。利用mediapipe检测手指关键点,并通过食指移动鼠标,当食指与中指距离小于阈值时触发点击事件。环境基于miniconda3,需创建虚拟环境并安装mediapipe、numpy、autopy和opencv等依赖。代码分为`AiVirtualMouse.py`和`HandTrackingModule.py`两个部分,前者用于实现鼠标控制逻辑,后者提供手势检测功能。运行时可能出现`PacketInvoked`错误,需修改`solution_base.py`文件第595行以解决。
1181 7
|
Android开发 iOS开发
ios后台播放声音的三种实现方式
ios后台播放声音的三种实现方式
2659 1
|
测试技术 数据安全/隐私保护 Docker
Docker部署开源项目Django-CMS企业内容管理系统
【5月更文挑战第20天】Docker部署开源项目Django-CMS企业内容管理系统
1109 1

热门文章

最新文章