作者:厚缊,中观经济咨询助理研究员,业余数据科学爱好者。博客:houyun.xyz

上篇:诹图系列(1): 简单条形图,这一篇文章主要解决如何用基础绘图系统绘制长标签条形图,再拓展到按照其他因子分类的叠加条形图。

长标签条形图


按照惯例,先上效果图。


数据及标签


原书作者没有提供ZA4753的原始数据,只能根据原图来手动整理。

## 条形的长度bar_data <- c(70.1, 84.7, 84.8, 35, 33.1, 47.2, 76.4)## 条形标签,不能放在一行的需要用“\n”手动换行myC_v159 <- "A working mother can establish just as warm and\nsecure environment as a non-working mother"myC_v160 <- "A pre-school child is likely to suffer if\nhis or her mother is working"myC_v161 <- "A job is alright but what most women\nreally want is a home and children"myC_v162 <- "Being a housewife is just as fulfilling as\nworking"myC_v163 <- "Having a job is the best way for a woman\nto be independent"myC_v164 <- "Both the husband and wife should contribute\nto the family income"myC_v165 <- "In general, fathers are as well suited to\nlook after their children as women"myC_v166 <- "Men should take as much responsibility\nas women for their household and children"names <- c(myC_v165, myC_v164, myC_v163, myC_v162, myC_v161, myC_v160, myC_v159)


图形设备


与前面的例子相同,继续使用cairo_pdf图形设备,背景色设置为grey98(98度灰)。因为本例条形标签比较长,需要占用较大的区域,所以图形宽度设置为13英寸。

barchart02 <- "your/figure/path/barcharts_simple.pdf"cairo_pdf(filename = barchart02, bg = "grey98", width = 13, height = 9)


全局绘图参数


mai第二个值,即左边绘图区边距设置为4英寸,保证条形标签不超出绘图区。

opar <- par()par(omi = c(0.75, 0.5, 1.2, 0.75),mai = c(0.35, 4, 0.75, 0),mgp = c(3, 3, 0),family = 'Arial')


主图及标签


主图设置和前例相同,不做过多解释。重点是标签部分,从图中发现第三个条形的颜色是品红色,标签是黑体,与其他条形存在明显的区别,所以为了避免后面叠加的品红色条形覆盖标签,这里先只标出正常字体的标签。

  • 先用ifelse()函数确定每个标签的字体,第三个是黑体,其他正常字体。

  • 然后再用for循环为每个条形添加标签,当循环到第三个时跳过。

# 主图y <- barplot(bar_data, col = 'grey', border = NA,              horiz = TRUE, names.arg = FALSE, axes = FALSE,             xlim = c(0, 100))# 标签font_name <- ifelse(1:7 == 3, 'Arial Black', 'Arial')for (i in 1:7) {if(i == 3) next()  text(-3, y[i], labels = names[i], xpd = TRUE,        adj = 1, family = font_name[i])  text(10, y[i], labels = format(bar_data[i], nsmall = 1), adj = 0.5,       family = font_name[i])}


背景色与特定条形高亮


背景是将X轴等分为5份,Y轴稍微超过坐标范围的矩形,填充色是半透明的浅蓝色。rect()函数用左下角(xleftybottom)和右上角(xrightytop)两个点确定一个矩形,当点坐标为向量时,绘制多个矩形。因此,使用点坐标向量的形式,可以一次绘制5个矩形,并分别制定每个矩形填充色。

xleft <- seq(0, 80, length.out = 5)xright <- seq(20, 100, length.out = 5)ybottom <- rep(0, 5)ytop <- rep(8.6, 5)bg_col <- ifelse(1:5 %% 2 == 1,                   rgb(191,239,255,80, maxColorValue=255),                 rgb(191,239,255,120, maxColorValue=255))rect(xleft, ybottom, xright, ytop, border = NA, col = bg_col)

将第三个条形绘制成高亮的品红色,方案就是除了第三个,其他条形长度设置为0,绘制颜色为品红的条形图并叠加到原条形上,然后为第三个条形添加黑体的标签。

bar_high_light <- ifelse(1:7 == 3, bar_data, 0)barplot(bar_high_light, horiz = TRUE, names.arg = FALSE, border = FALSE,col = rgb(255, 0, 210, maxColorValue = 255), axes = FALSE,add = TRUE)text(-3, y[3], labels = names[3], xpd = TRUE,adj = 1, family = font_name[3])text(10, y[3], labels = format(bar_data[3], nsmall = 1), adj = 0.5,family = font_name[3])


其他细节图形标记


其他图形细节和前例几乎没有差别,不一一进行解释。

arrows(50, 0, 50, 8.7, lwd = 1.5, col = 'skyblue', length = 0, xpd = TRUE)arrows(50, -0.25, 50, 0, lwd = 3, col = 'black', length = 0, xpd = TRUE)arrows(50, 8.7, 50, 8.95, lwd = 3, col = 'black', length = 0, xpd = TRUE)text(48, 8.7, labels = 'Mojority', adj = 1, cex = 0.9,      font = 3, xpd = TRUE)text(52, 8.7, labels = '50%', adj = 0, cex = 0.9,      font = 3, xpd = TRUE)text(100, 8.7, labels = 'all value in percent', adj = 1,      cex = 0.9, font = 3, xpd = TRUE)mtext(seq(0, 100, length.out = 6),       at = seq(0, 100, length.out = 6), side = 1,      line = 0, cex = 0.9)mtext('It is often said that attitudes towards gender roles are changing',      side = 3, line = 2, cex = 1.8, adj = 0,       family = 'Arial Black', outer = TRUE)mtext('Agree strongly/Agree', side = 3, line = 0, cex = 1.5,       adj = 0, outer = TRUE)mtext('Source: European Values Study 2008 Germany, ZA4753. www.gesis.org. Design: Stefan Fichtel, ixtract',      side = 1, line = 0.7, adj = 1, cex = 0.9,       outer = TRUE)dev.off()par(opar)


堆叠条形图


这一列只放效果图和代码,有兴趣可以自己动手绘制。

barchart03 <- 'your/figure/path/barcharts_simple.pdf'cairo_pdf(barchart03, bg = 'grey98', width = 13, height = 10.5)data <- c(5,27,42,23,3,3,38,47,9,3,3,44,41,10,2,7,7,28,32,26,7,6,27,35,25,5,14,33,31,17,3,39,36,15,7)za4753 <- t(matrix(data, nrow = 7, byrow = TRUE))responses <- c("n.a./don’t know", "agree strongly", "agree", "disagree", "disagree strongly")opar <- par()par(mai = c(1.4, 3.8, 0.5, 0),omi = c(0, 0.25, 2.1, 0.75),family = 'Arial',lheight = 1.3,las = 1)# labelsmyC_v159 <- "A working mother can establish just as warm and\nsecure environment as a non-working mother"myC_v160 <- "A pre-school child is likely to suffer if\nhis or her mother is working"myC_v161 <- "A job is alright but what most women\nreally want is a home and children"myC_v162 <- "Being a housewife is just as fulfilling as\nworking"myC_v163 <- "Having a job is the best way for a woman\nto be independent"myC_v164 <- "Both the husband and wife should contribute\nto the family income"myC_v165 <- "In general, fathers are as well suited to\nlook after their children as women"myC_v166 <- "Men should take as much responsibility\nas women for their household and children"names <- c(myC_v165, myC_v164, myC_v163, myC_v162, myC_v161, myC_v160, myC_v159)# 主图## 颜色col01 <- rgb(0,208,226, maxColorValue =255)col02 <- rgb(109,221,225, maxColorValue =255)col03 <- rgb(255,138,238, maxColorValue =255)col04 <- rgb(255,0,210, maxColorValue =255)col_name <- c('grey', col01, col02, col03, col04)##绘图y <- barplot(za4753, names.arg = names, border = NA, horiz = TRUE,col = col_name, axes = FALSE, xlim = c(0, 100), cex.names = 1.1)#其它绘图元素xl_mark <- c(2, 8, 37, 64, 98)yl_mark <- rep(9.0, 5)xl_lab <- c(-1, 25, 46, 61, 95)yl_lab <- rep(9.0, 5)points(xl_mark, yl_mark, pch = 15, cex = 4, col = col_name, xpd = TRUE)text(xl_lab, yl_lab, labels = responses, adj = 1, xpd = TRUE, font = 3)x_label <- c(0, 20, 40, 60, 80, 100)mtext(x_label, side = 1, at = x_label, line = 0, cex = 0.95)mtext('It is often said that attitudes towards gender roles are changing',side = 3, line = 6.5, cex = 1.8, adj = 0, family = 'Arial Black', outer = TRUE)mtext('Agree strongly/Agree', side = 3, line = 3.5, cex = 1.5, adj = 0, outer = TRUE)mtext('Source: European Values Study 2008 Germany, ZA4753. www.gesis.org. Design: Stefan Fichtel, ixtract',side = 1, line = 4.5, adj = 1, cex = 0.9)mtext('all value in percent', side = 1, line = 1.5, cex = 0.9, adj = 1, font = 3)mtext('N = 2075', side = 1, line = 1.5, cex = 0.9, adj = 0, font = 3)dev.off()par(opar)

——————————————

往期精彩:

  • visreg:带你玩遍模型可视化

  • 方差分析:单因子与多因子

  • 真实经历,互联网大厂升职加薪那些事

  • R语言中文社区2018年终文章整理(作者篇)

  • R语言中文社区2018年终文章整理(类型篇)

诹图系列(2): 堆积条形图相关推荐

  1. 诹图系列(1): 简单条形图

    欢迎关注天善智能,我们是专注于商业智能BI,人工智能AI,大数据分析与挖掘领域的垂直社区,学习,问答.求职一站式搞定! 对商业智能BI.大数据分析挖掘.机器学习,python,R等数据领域感兴趣的同学 ...

  2. python plotly 平均值折线图_如何使用Plotly带有折线图的堆积条形图?

    我想使用plotly和iPython创建一个带有线条图的条形图,如上例所示.另一方面,我希望条形图是一个水平堆积条形图,如下例中使用plotly和iPython.我该怎么做呢? y_saving_ye ...

  3. JavaFX图表(八)之堆积条形图

    翻译自   JavaFX - 堆积条形图 StackedBarChart是BarChart的变体,它绘制了指示类别数据值的条形图.条形可以是垂直的或水平的,这取决于哪个轴是类别轴.每个系列的栏位于上一 ...

  4. ECharts系列 - 柱状图(条形图)实例一

    ECharts主页:  http://echarts.baidu.com/index.html ECharts-2.1.8下载地址:  http://echarts.baidu.com/build/e ...

  5. 什么是百分比堆积条形图?

    条形图实际上范围很广,它是以横置图形展示数据的一种图表类型.百分比堆积条形图即以堆积条形图的形式来显示多个数据序列,但是每个堆积元素的累积比例始终总计为 100%.它主要用于显示一段时间内的多项数据占 ...

  6. python 条形图_Python数据可视化:基于matplotlib绘制「堆积条形图」

    简介 堆积条形图作为条形图的一种,能够显示单个项目与整体之间的关系.不仅可以直观的看出每个系列的值,还能够反映出系列的总和,尤其是当需要看某一项目的总和以及其组成各系列值的比重时,最适合使用堆积条形图 ...

  7. origin 双Y轴堆积条形图

    origin 做双Y轴堆积条形图 方法步骤 首先打开Origin软件,按照上述格式输入数据.那么我们基本的思路是,将"左"设置为左Y轴,将"右"设置为右Y轴. ...

  8. python 条形图 负值_使用python matplotlib获取正负值的堆积条形图

    我正在尝试使用python Matplotlib绘制堆积的条形图,并且我要绘制正值和负值.我看过其他文章,谈论如何用正值和负值绘制堆积的条形图,但是没有一个使用Matplotlib完成,所以我找不到解 ...

  9. Power BI--如何给堆积条形图增加总计

    在Power BI报表中,堆积条形图的使用场景还是比较多的,比如下图的销售趋势图表中,既可以看到整体的销售趋势,又可以看到不同品牌(通过颜色来区分)的销售趋势. 但是,如果想知道每一周的总体销售额,这 ...

  10. 数据挖掘与分析应用:tableau可视化数据分析,仪表盘,折线图,饼图,条形图,地图,散点图,区域图,表格,数据分析引用

    数据挖掘与分析应用:tableau可视化数据分析,仪表盘,折线图,饼图,条形图,地图,散点图,区域图,表格,数据分析引用 2022找工作是学历.能力和运气的超强结合体,遇到寒冬,大厂不招人,可能很多算 ...

最新文章

  1. zend studio 远程调试 php
  2. win10连接蓝牙耳机_苹果手机蓝牙耳机无法连接怎么办?
  3. winform调用webservice增删查改_教你分别用数据库与云函数实现“增删查改”
  4. 腾讯工作心得:原型该画到什么程度?
  5. 钱与命的八句真实话(图)
  6. python心理学实验程序_心理学实验程序编程(python)
  7. 再谈Windows 2000安全技术
  8. NOI2011 阿狸的打字机题解
  9. angular模态框位置_angular $modal 模态框
  10. flink的Yarn模式
  11. python语言适用于哪些领域_Python用于哪些领域
  12. 浅谈SSD应用和发展趋势
  13. 在qemu下实战linux内核
  14. 智慧城市大数据创新联盟落户高新区
  15. Sketch快捷键大全 Sketch如何自定义快捷键?
  16. 【2020年高被引学者】 田奇 华为
  17. 关于码距、检错、纠错
  18. 如何使用KMS(2)
  19. MATHTYPE安装出现问题:无法打开要写入的文件;MathType打开word时“安全警告,宏已被禁用”;mathtype与AXmath不能同时使用
  20. 通过adb操作安卓亮屏、设置背光亮度、解锁、打开app

热门文章

  1. Linux怎么取消软链接
  2. 电脑e盘里的文件误删了 如何恢复教程分享
  3. 重磅!微软在 GitHub 又一开源力作面世,代号「女娲」!
  4. 微服务设计 10 大反模式和陷阱!
  5. 推荐几款压箱底的IDEA插件,撸码利器
  6. 这是目前为止5G最完整的PPT
  7. 六面美团后,我有一个重要的发现...
  8. 【重大改革】吴恩达:孩子会识字后立马教她Python!
  9. 浅谈web架构之架构设计
  10. 19 个如此好玩有趣的 Linux 命令,你玩过几个呢?