要点
- 轴标签过长则可以把数值放在柱子里面
- 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
网友评论