柱状图2之二分类变量

作者: x2yline | 来源:发表于2017-12-01 20:38 被阅读16次

要点

  • 轴标签过长则可以把数值放在柱子里面
  • memisc包读取spss数据(由于spss数据作者未提供---已无法找到,所以数据为手动输入)
  • 做线段可以使用arrow函数
  • border=NA设置不显示边框,如果设置为数值则边框颜色为设置对应的palette颜色
  • las表示轴标签与轴的方向关系(0表示与轴平行,1表示总是横向,2表示与轴垂直,3表示总是纵向),同时par中定义的该参数也作用于mtext函数
  • mtext函数中text参数为vector时,指定text的位置用at参数
  • xpd参数表示是否不对图形区域进行限制,默认为FALSE表示仅允许plot区域进行添加图形元素
  • names.arg:相当于borplot中x轴的标签,设置为FALSE表示不绘制轴标签
# 定义标签名称
myC_v159 <- "A working mother can establish just as warm and\nsecure an 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"
mynames <- c(myC_v165, myC_v164, myC_v163, myC_v162, myC_v161, 
             myC_v160, myC_v159)

# 导入数据
# library(memisc)
# ZA4753<-spss.system.file('myData/ZA4753_v1-1-0.sav')
# myData<-subset(ZA4753,select=c(v159,v160,v161,v162,v163,v164,v165))
# attach(myData) z<-NULL y<-table(as.matrix(v165))
# z<-c(z,100*(y['1']+y['2'])/sum(y))
# y<-table(as.matrix(v164))
# z<-c(z,100*(y['1']+y['2'])/sum(y))
# y<-table(as.matrix(v163))
# z<-c(z,100*(y['1']+y['2'])/sum(y))
# y<-table(as.matrix(v162))
# z<-c(z,100*(y['1']+y['2'])/sum(y))
# y<-table(as.matrix(v161))
# z<-c(z,100*(y['1']+y['2'])/sum(y))
# y<-table(as.matrix(v160))
# z<-c(z,100*(y['1']+y['2'])/sum(y))
# y<-c(0,table(as.matrix(v159)))
# z<-c(z,100*(y['1']+y['2'])/sum(y))
z <- c(70.1, 84.7, 84.8, 35, 33.1, 47.2, 76.4)

# add_fonts("Lato")
# 导入字体(需在windows下预先安装Lato字体,以图形方式显示,若为cairo_pdf输出则可以省略该步),add_fonts函数为自定义的,
# 详细代码参考:http://www.jianshu.com/p/d6636950e25d

# lheight表示lineheight表示行高,默认值为1
# las 
# numeric in {0,1,2,3}; the style of axis labels.
# 0: always parallel to the axis [default], 与轴平行
# 1: always horizontal, 总是横向
# 2: always perpendicular to the axis, 总是与轴垂直
# 3: always vertical. 总是纵向
# Also supported by mtext. las也会影响mtext文字的排列方向
# Note that string/character rotation via argument srt(string rotation in degrees) to par does not affect the axis labels.
par(omi=c(0.65, 0.75, 1.25, 0.75), mai=c(0.9, 3.85, 0.55, 0), lheight=1.15,
    family="Lato Light", las=1, srt=0)

# names.arg:相当于borplot中x轴的标签
# a vector of names to be plotted below each bar or group of bars. 
# If this argument is omitted, then the names are taken from the names attribute of height 
# if this is a vector, or the column names if it is a matrix.

# border:边框设置为数值代表默认palette中的颜色
bp <- barplot(z, names.arg = F, horiz = T, border = NA, 
              xlim = c(0, 100), col = "grey", axes = F, family = "Lato Regular")

## 背景矩形的颜色
rect(0, -0.1, 20, 8.6, col = rgb(191, 239, 255, 80, maxColorValue = 255), 
     border = NA)
rect(20, -0.1, 40, 8.6, col = rgb(191, 239, 255, 120, maxColorValue = 255), 
     border = NA)
rect(40, -0.1, 60, 8.6, col = rgb(191, 239, 255, 80, maxColorValue = 255), 
     border = NA)
rect(60, -0.1, 80, 8.6, col = rgb(191, 239, 255, 120, maxColorValue = 255), 
     border = NA)
rect(80, -0.1, 100, 8.6, col = rgb(191, 239, 255, 80, maxColorValue = 255), 
     border = NA)

# 特别标注的柱子
myColour <- rgb(255, 0, 210, maxColorValue = 255)
z2 <- c(0, 0, 84.81928, 0, 0, 0, 0)
bp <- barplot(z2, names.arg = F, horiz = T, border = NA, 
              xlim = c(0, 100), col = myColour, axes = F, add = T)

# 标注y轴和柱高数值
for (i in 1:length(mynames)) {
  if (i == 3) {
    myFont <- "Lato Bold"
  } else {
    myFont <- "Lato Light"
  }
  text(-3, bp[i], mynames[i], xpd = T, adj = 1, family = myFont, 
       cex = 1)
  text(10, bp[i], format(round(z[i], 1), nsmall = 1), family = myFont, 
       cex = 1.25, col = ifelse(i == 3, "white", "black"))
}

# 线段绘制
arrows(50, -0.1, 50, 8.8, lwd = 1.5, length = 0, xpd = T, col = "skyblue3")
arrows(50, -0.25, 50, -0.1, lwd = 5, length = 0, xpd = T)
arrows(50, 8.8, 50, 8.95, lwd = 5, length = 0, xpd = T)

# 标注
text(48, 8.9, "Majority", adj = 1, xpd = T, cex = 0.9, font = 3)
text(52, 8.9, "50%", adj = 0, xpd = T, cex = 0.9, family = "Lato Bold", 
     font = 3)
text(100, 8.9, "all values in percent", adj = 1, xpd = T, cex = 0.9, 
     font = 3)

# x轴标签,at表示个文字位置,可以相当于adj的定位作用
mtext(c(0, 20, 40, 60, 80, 100), at = c(0, 20, 40, 60, 80, 100), 
      side = 1, line = 0.75)

# 标题
mtext("It is often said that attitudes towards gender roles are changing", 
      3, line = 2.2, adj = 0, cex = 1.8, family = "Lato Black", 
      outer = T)
mtext("Agree strongly / agree ", 3, line = 0, adj = 0, cex = 1.5, 
      outer = T)
mtext("Source: European Values Study 2008 Germany, ZA4753. www.gesis.org. Design: Stefan Fichtel, ixtract", 
      1, line = 0, adj = 1, cex = 0.95, outer = T, font = 3)
chat2.png

相关文章

  • 柱状图2之二分类变量

    要点 轴标签过长则可以把数值放在柱子里面 memisc包读取spss数据(由于spss数据作者未提供---已无法找...

  • bokeh绘制柱状图——堆叠图——直方图

    1、单系列柱状图-vbar 2.单系列柱状图 - 分类设置标签-ColumnDataSource 2、多系列柱状图...

  • Stata-柱状图

    根据Stata的帮助文档,我罗列部分柱状图的绘制方案。 分组累积柱状图 数据格式要求:分组变量与几个待加总变量,比...

  • 2018-11-09 R数据可视化学习 -了解数据特征

    1.关于变量的类型的区别(数值、分类) 注:分类型变量,如苹果和梨不可比较 2. 关于数值变量 2.1 了解均值、...

  • 2020-06-09 变量和函数

    变量: (1) 根据数据类型分类:1. 基本数据类型变量 2. 引用数据类型变量 (2) 根据变量的位置分...

  • 生存分析(2)——基线图

    因变量 自变量 自变量类型 分类变量分类变量包含有限的分类数或可区分组数。分类数据可能不是逻辑顺序,其包括二分类和...

  • 从零开始学Python可视化(四): 并列柱状图

    堆积柱状图有堆积柱状图的好处,比如说我们可以很方便地看到多分类总和的趋势。 但是我们发现,在堆积柱状图中,由于基底...

  • bash编程-Shell变量

    bash中,所有变量的值默认均为字符串。 1. 变量操作 调用变量 查看变量(所有类型) 删除变量 2. 变量分类...

  • 从零建立回归模型步骤 (2020.04.17)

    1. 明确需求(因变量Y) 2. 数据清洗(缺失值、异常值、分类变量) 异常值:默认三倍标准差之外分类变量:多为汉...

  • 图表优化

    1.进入页面默认显示柱状图。选择柱状图type后,更改日期默认显示之前type的柱状图。 在js中设置全局变量va...

网友评论

    本文标题:柱状图2之二分类变量

    本文链接:https://www.haomeiwen.com/subject/fltgbxtx.html