R语言 双坐标轴组合图形可视化实现
“ 数据可视化过程中,经常遇到两种不同类型图表组合的情况,就是所谓的双坐标轴组合图。最近学习中遇到了此问题,特学习和大家分享,部分内容有个人改进哟”
01
—
效果图
02
—
# 1、用法/Usage:
twoord.plot(lx,ly,rx,ry,data=NULL,main="",xlim=NULL,lylim=NULL,rylim=NULL,
mar=c(5,4,4,4),lcol=1,rcol=2,xlab="",lytickpos=NA,ylab="",ylab.at=NA,
rytickpos=NA,rylab="",rylab.at=NA,lpch=1,rpch=2,
type="b",xtickpos=NULL,xticklab=NULL,halfwidth=0.4,axislab.cex=1,
do.first=NULL,xaxt="s",...)
# 2、参数解释:
1、lx,ly,rx,ry:左坐标轴和右坐标轴的值
注意:lx和rx值至少相邻,并且可能重叠
2、data:数据框
3、main:标题
4、xlim、lylim,rylim:限制横坐标值、左右纵坐标值的范围
5、mar:设置图形边界距,默认值为(5,4,4,4)
6、lcol、rcol:左右坐标轴的颜色
7、xlab:设置横坐标轴标签
8、lytickpos:设置左坐标轴刻度标签的位置
9、ylab:设置左坐标轴标签
10、ylab.at:设置左坐标轴标签位置
11、rytickpos:设置右坐标轴刻度标签的位置
12、rylab:设置右坐标轴标签
13、rylab.at:设置右坐标轴标签位置
14、type:指定图形类型 type
"p" for points,"l" for lines,"b" for both,"c" for the lines part alone of "b",
"o" for both ‘overplotted’,"h" for ‘histogram’ like (or ‘high-density’) vertical lines,
"s" for stair steps,"S" for other steps, see ‘Details’ below,
"n" for no plotting.
15、xtickpos:设置横坐标轴刻度标签位置
16、xticklab:设置横坐标轴刻度标签
……
详见R-gui帮助文档,代码:help(plotrix)
03
—
上案例
案例1:左右轴的x值包含
xval1 <- 2:10
xval2 <- 1:15
going_up <- seq(3,7,by=0.5)+rnorm(9)
going_down <- rev(60:74)+rnorm(15)
twoord.plot(lx = xval1,ly = going_up, rx = xval2, ry = going_down,xlab="Sequence",
ylab="Ascending values",rylab="Descending values",lcol=4,
main="Plot with two ordinates - points and lines",
do.first="plot_bg();grid(col=\"white\",lty=2)")
案例2:左右轴的x值重叠(一致)
xval <- seq.Date(as.Date("2017-01-01"),
as.Date("2017-01-15"), by="day")
going_up<-seq(3,7,length.out = 15)+rnorm(15)
going_down<-rev(60:74)+rnorm(15)
twoord.plot(xval, going_up, xval, going_down,xlab="Sequence",
ylab="Ascending values",rylab="Descending values",lcol=4,
main="Plot with two ordinates - points and lines",
xtickpos=as.numeric(xval), xticklab=as.character(xval),
do.first="plot_bg();grid(col=\"white\",lty=1)")
# 注释:xtickpos=as.numeric(xval), xticklab=as.character(xval)两个参数是控制x轴显示日期的关键,请知晓~
案例3:不带数值标签的混合类型双坐标图
## 折线&柱形混合双坐标图形(不含图形数值标签)
twoord.plot(xval1, going_up, xval2, going_down, xlab="Sequence",
lylim=c(-3,8), rylim=c(50,100), type=c("l","bar"),
ylab="Ascending values", rylab="Descending values",
text = list(going_up,going_down),
lytickpos=3:7, rytickpos=seq(55,75,by=5), ylab.at=5,rylab.at=65,
main="Lines on left axis, bars on right axis(不含点数值标签)",
lcol="red", rcol=4,do.first="plot_bg(\"white\")")
案例4:带数值标签的混合类型双坐标图(值得学习)
## 折线&柱形混合双坐标图形(含图形数值标签)
twoord.plot(xval1, going_up, xval2, going_down, xlab="Sequence",
lylim=c(-3,8), rylim=c(50,100), type=c("l","bar"),
ylab="Ascending values", rylab="Descending values",
text = list(going_up,going_down),
lytickpos=3:7, rytickpos=seq(55,75,by=5), ylab.at=5,rylab.at=65,
main="Lines on left axis, bars on right axis(含点数值标签)",
lcol="red", rcol=4,do.first="plot_bg(\"white\")")
text(xval1, going_up,as.numeric(round(going_up)))
text(xval2,
(going_down-min(c(50,100)))/(max(c(50,100))-min(c(50,100)))*(max(c(-3,8))-min(c(-3,8)))+min(c(-3,8)+0.5),
round(going_down))
#注意:
# 1.添加left线形图形标签可直接使用text函数,
# 2.但right柱形图形标签不适合直接使用text标签,需要将右侧ry值等比例转换为ly值
# 3.转换公式:(ry-min(rylim))/(max(rylim)-min(rylim))* (max(lylim)-min(lylim))+ min(lylim)+位置调整值
案例5:柱形图&概率密度混合双坐标图
xhist<-hist(rnorm(100),plot=FALSE)
xdens<-dnorm(seq(-3,3,by=0.05))
twoord.plot(xhist$mids,xhist$counts,seq(-3,3,by=0.05),
xdens,type=c("bar","l"),lcol=4,rcol=2,ylab="Counts",
rylab="Density",main="Histogram and density curve",
halfwidth=0.2,lylim=c(0,max(xhist$counts)+1),rylim=c(0,0.45),lwd=2)
04
—
源代码
1、更多学习twoord.stackplot
2、源数据及R代码:
链接: https://pan.baidu.com/s/1lMnHySqzwV2uxMqroINM3g
提取码: 8bks