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

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

引言

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

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

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

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

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

可视化系列汇总——相关关系图形

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

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

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

系列目录



本文主要介绍第五部分:组成成分图形。

5 组成

5.1 华夫饼图

华夫图可以显示总体的组成成分。

var <- mpg$class  # the categorical data 
## Prep data (nothing to change here)
nrows <- 10
df <- expand.grid(y = 1:nrows, x = 1:nrows)
categ_table <- round(table(var) * ((nrows*nrows)/(length(var))))
df$category <- factor(rep(names(categ_table), categ_table))  
# NOTE: if sum(categ_table) is not 100 (i.e. nrows^2), it will need adjustment to make the sum to 100.
## Plot
ggplot(df, aes(x = x, y = y, fill = category)) + 
        geom_tile(color = "black", size = 0.5) +
        scale_x_continuous(expand = c(0, 0)) +
        scale_y_continuous(expand = c(0, 0), trans = 'reverse') +
        scale_fill_brewer(palette = "Set3") +
        labs(title="Waffle Chart", subtitle="'Class' of vehicles",
             caption="Source: mpg") + 
        theme(panel.border = element_rect(size = 2),
              plot.title = element_text(size = rel(1.2)),
              axis.text = element_blank(),
              axis.title = element_blank(),
              axis.ticks = element_blank(),
              legend.title = element_blank(),
              legend.position = "right")



华夫饼图

5.2 饼图

library(ggplot2)
theme_set(theme_classic())
# Source: Frequency table
df <- as.data.frame(table(mpg$class))
colnames(df) <- c("class", "freq")
pie <- ggplot(df, aes(x = "", y=freq, fill = factor(class))) + 
  geom_bar(width = 1, stat = "identity") +
  theme(axis.line = element_blank(), 
        plot.title = element_text(hjust=0.5)) + 
  labs(fill="class", 
       x=NULL, 
       y=NULL, 
       title="Pie Chart of class", 
       caption="Source: mpg")
pie + coord_polar(theta = "y", start=0)



饼图

5.3 条形图

#prep frequency table
freqtable <- table(mpg$manufacturer)
df <- as.data.frame.table(freqtable)
head(df)


library(ggplot2)
theme_set(theme_classic())
# Plot
g <- ggplot(df, aes(Var1, Freq))
g + geom_bar(stat="identity", width = 0.5, fill="tomato2") + 
      labs(title="Bar Chart", 
           subtitle="Manufacturer of vehicles", 
           caption="Source: Frequency of Manufacturers from 'mpg' dataset") +
      theme(axis.text.x = element_text(angle=65, vjust=0.6))


条形图

# From on a categorical column variable
g <- ggplot(mpg, aes(manufacturer))
g + geom_bar(aes(fill=class), width = 0.5) + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
  labs(title="Categorywise Bar Chart", 
       subtitle="Manufacturer of vehicles", 
       caption="Source: Manufacturers from 'mpg' dataset")


条形图

参考资料

[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

目录
相关文章
|
8月前
|
数据可视化 数据挖掘 数据处理
R绘图 | 浅谈散点图及其变体的作图逻辑
R绘图 | 浅谈散点图及其变体的作图逻辑
153 0
|
10月前
|
数据可视化
绘制热图时看不出颜色差异?四种方式转换处理使结果显而“易”见
绘制热图时看不出颜色差异?四种方式转换处理使结果显而“易”见
18534 2
|
1月前
|
数据可视化 定位技术
r语言空间可视化绘制道路交通安全事故地图
r语言空间可视化绘制道路交通安全事故地图
|
1月前
|
前端开发 JavaScript
图形应用
图形应用
20 3
|
9月前
|
机器学习/深度学习 人工智能 图形学
计算机图形学中的图形输入详解
计算机图形学中的图形输入详解
198 2
|
10月前
|
数据可视化 数据挖掘 Linux
科研绘图丨使用R语言Pheatmap包快速绘制基因表达量热图的方法,支持聚类和配色自定义修改
科研绘图丨使用R语言Pheatmap包快速绘制基因表达量热图的方法,支持聚类和配色自定义修改
|
数据可视化 数据挖掘
常用 7 大类型图形可视化——偏差关系图形
常用 7 大类型图形可视化——偏差关系图形
176 0
|
数据可视化 数据挖掘 数据格式
常用 7 大类型图形可视化——变化趋势图形
常用 7 大类型图形可视化——变化趋势图形
68 0
|
数据可视化 数据挖掘
常用 7 大类型图形可视化——分布
常用 7 大类型图形可视化——分布
114 0
|
数据可视化 数据挖掘
可视化系列汇总——相关关系图形
可视化系列汇总——相关关系图形
103 0