其他
构造方块条形图
今天给大家分享一种特殊条形图的画法,它把条形图的每个条分成若干个小矩形.基本思路是把每个条形图的y值按比例分成若干分,每一份代表该条所对应条形图中两个小矩形的面积和,其他的主要是配色问题.我用一个实例教大家如何绘制这样的条形图,然后我会给出一个封装函数,大家只要输入配色就能绘制各种不同的条形图
数据准备
大家需要注意的是df3和dfps的构造方法,df2是把条形图的y值分组进行均分, 每份用1表示.dfps是统计每组分割的个数.
library(tidyverse)library(cowplot)
df=data.frame(score=c(11,32,46,12,19,42,31,7,34,29,27,10, 23,41,29,7),
sex=rep(c('M','F'),rep(8,2)),
grade=rep(paste0('grade_',LETTERS[1:4]),rep(4,4)),
class=rep(paste0('class_',LETTERS[1:4]),4),
case=rep(paste0('case_',LETTERS[1:8]),2))
#数据处理
df=df%>%group_by(class)%>%mutate(scores=round(score/sum(score)*100,2))
#df$scores= round(df$score/sum(df$score)*100)
df2=df[df$sex=='M',]library(plyr)
df3=ddply(df2, .(case), transform, scores1 = rep(1, score/3))
dfps <- data.frame(x1 = 1:8, x2 = 1:8, y1 = rep(0,8), y2 = count(df3,vars ='case')$freq)
一个实例
psx <- ggplot(df3, aes(x=case, y = scores1, colour=case)) +
geom_bar(width = 0.5, stat = "identity",fill='white')+
geom_bar(position = "stack",width = 1,
stat = "identity", fill = NA, colour = "black")+
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "segment"), data = dfps)+
scale_color_manual(values = rep('black',9))+
theme(axis.text.x=element_text(angle = 45,vjust = 0.5,
colour = 'white',face='bold'),
axis.text.y = element_text(colour = 'white',face='bold'),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line = element_line(size = 1,colour = 'white'),
axis.ticks = element_line(size = 1,colour = 'white'),
legend.position = 'none',
panel.background = element_rect(fill = 'black'),
panel.grid.major = element_line(colour = 'black'),
panel.grid.minor = element_line(colour = 'black'),
plot.background = element_rect(fill = 'black')
)
psx
构造分装函数
br_fun=function(x,w){
cols1=x[1]; cols2=x[2];cols3=x[3];cols4=x[4];cols5=x[5];cols6=x[6]
psx2 <- ggplot(df3, aes(x=case, y = scores1, colour=case)) +
geom_bar(width = w, stat = "identity",fill=cols1)+
geom_bar(position = "stack",width = w,
stat = "identity", fill = NA, colour = cols2)+
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "segment"), data = dfps)+
scale_color_manual(values = rep(cols2,9))+
theme(axis.text.x=element_text(angle = 45,vjust = 0.5,
colour = cols3,face='bold'),
axis.text.y = element_text(colour = cols3,face='bold'),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line = element_line(size = 1,colour = cols5),
axis.ticks = element_line(size = 1,colour = cols6),
legend.position = 'none',
panel.background = element_rect(fill = cols4),
panel.grid.major = element_line(colour = cols4),
panel.grid.minor = element_line(colour = cols4),
plot.background = element_rect(fill = cols4)
) return( psx2)
}
绘制图形
#黑白(1)
p1=br_fun(c('white','black','white','black','black','black'),0.5)
#黑白(2)p2=br_fun(c('black','white','white','black','black','black'),1)
#黑白(3)p3=br_fun(c('black','white','black','white','white','white'),1)
#黑白(4)p4=br_fun(c('white','black','black','white','white','white'),0.5)
grid.newpage()
#新建页面
pushViewport(viewport(layout = grid.layout(2,2)))
#将页面分成2*2矩阵
vplayout <- function(x,y){
viewport(layout.pos.row = x, layout.pos.col = y)
}
print(p1, vp = vplayout(1,1))
#在(1,1)的位置画图p1
print(p4, vp = vplayout(1,2))
#将(1,2)的位置画图p4
print(p3, vp = vplayout(2,1))
#在(2,1)的位置画图p3
print(p2, vp = vplayout(2,2))
#在(2,2)的位置画图p2
饼图
# 对应饼图
p11 = p1+theme(axis.text.x =element_text(colour = 'black'),
axis.text.y =element_text(colour = 'black'))+
coord_polar()
p21 = p2+theme(axis.text.x =element_text(colour = 'black'),
axis.text.y =element_text(colour = 'black'))+
coord_polar()
p31 = p3+theme(axis.text.x =element_text(colour = 'white'),
axis.text.y =element_text(colour = 'white'))+
coord_polar()
p41 = p4+theme(axis.text.x =element_text(colour = 'white'),
axis.text.y =element_text(colour = 'white'))+
coord_polar()
grid.newpage()
#新建页面
pushViewport(viewport(layout = grid.layout(2,2)))
#将页面分成2*2矩阵vplayout <- function(x,y){
viewport(layout.pos.row = x, layout.pos.col = y)
}
print(p11, vp = vplayout(1,1))
#在(1,1)的位置画图p11
print(p41, vp = vplayout(1,2))
#将(1,2)的位置画图p41
print(p31, vp = vplayout(2,1))
#在(2,1)的位置画图p31
print(p21, vp = vplayout(2,2))
#在(2,2)的位置画图p21
如需转载请联系EasyCharts团队!
微信后台回复“转载”即可!
【书籍推荐】《Excel 数据之美--科学图表与商业图表的绘制》
【手册获取】国内首款-数据可视化参考手册:专业绘图必备
【必备插件】 EasyCharts -- Excel图表插件
【网易云课堂】 Excel 商业图表修炼秘笈之基础篇