我正在对银行业的非违约者和违约者进行一些研究。在这种情况下,我将他们的分布相对于条形图中的某个分数进行绘图。评分越高,信用评级越高。向ggplot2栏添加垂直线图
由于与非默认值的数量相比,默认值的数量非常有限,因此绘制默认值和非默认值在同一条形图上并不是很给力,因为您几乎看不到默认值。然后,我根据违纪者的分数创建第二个条形图,但是与违纪者和非违约者的分数的完整条形图相同。然后,我想向第一个条形图添加垂直线,指示最高违规者得分位置以及最低违规者得分所在的位置。那就是要弄清楚违约者的分布在哪里适合违约者和非违约者的整体分配。
下面是我正在使用的代码替换(种子)随机数据。
library(ggplot2)
#NDS represents non-defaults and DS defaults on the same scale
#although here being just some random normals for the sake of simplicity.
set.seed(10)
NDS<-rnorm(10000,sd=1)-2
DS<-rnorm(100,sd=2)-5
#Cutoffs are constructed such that intervals of size 0.3
#contain all values of NDS & DS
minCutoff<--9.3
maxCutoff<-2.1
#Generate the actual interval "bins"
NDS_CUT<-cut(NDS,breaks=seq(minCutoff, maxCutoff, by = 0.3))
DS_CUT<-cut(DS,breaks=seq(minCutoff, maxCutoff, by = 0.3))
#Manually generate where to put the vertical lines for min(DS) and max(DS)
minDS_bar<-levels(cut(NDS,breaks=seq(minCutoff, maxCutoff, by = 0.3)))[1]
maxDS_bar<-levels(cut(NDS,breaks=seq(minCutoff, maxCutoff, by = 0.3)))[32]
#Generate data frame - seems stupid, but makes sense
#when the "real" data is used :-)
NDSdataframe<-cbind(as.data.frame(NDS_CUT),rep(factor("State-1"),length(NDS_CUT)))
colnames(NDSdataframe)<-c("Score","Action")
DSdataframe<-cbind(as.data.frame(DS_CUT),rep(factor("State-2"),length(DS_CUT)))
colnames(DSdataframe)<-c("Score","Action")
fulldataframe<-rbind(NDSdataframe,DSdataframe)
attach(fulldataframe)
#Plot the full distribution of NDS & DS
# with geom_vline(xintercept = minDS_bar) + geom_vline(xintercept = maxDS_bar)
# that unfortunately does not show :-(
fullplot<-ggplot(fulldataframe, aes(Score, fill=factor(Action,levels=c("State-2","State-1")))) + geom_bar(position="stack") + opts(axis.text.x = theme_text(angle = 45)) + opts (legend.position = "none") + xlab("Scoreinterval") + ylab("Antal pr. interval") + geom_vline(xintercept = minDS_bar) + geom_vline(xintercept = maxDS_bar)
#Generate dataframe for DS only
#It might seem stupid, but again makes sense
#when using the original data :-)
DSdataframe2<-cbind(as.data.frame(DS_CUT),rep(factor("State-2"),length(DS_CUT)))
colnames(DSdataframe2)<-c("theScore","theAction")
#Calucate max number of observations to adjust bar plot of DS only
myMax<-max(table(DSdataframe2))+1
attach(DSdataframe2)
#Generate bar plot of DS only
subplot<-ggplot(fulldataframe, aes(theScore, fill=factor(theAction))) + geom_bar (position="stack") + opts(axis.text.x = theme_text(angle = 45)) + opts(legend.position = "none") + ylim(0, myMax) + xlab("Scoreinterval") + ylab("Antal pr. interval")
#plot on a grid
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1)))
vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
print(fullplot, vp = vplayout(1, 1))
print(subplot, vp = vplayout(2, 1))
#detach dataframes
detach(DSdataframe2)
detach(fulldataframe)
此外,如果有人有我如何对齐一个想法绘制,使正确的时间间隔仅仅低于/高于对方的网格划分
希望有人能够帮助!
由于提前,
基督教
谢谢!精彩的作品!你有关于如何对齐网格上的两个图的想法吗?也就是说,在两个地块上,区间X低于区间X,区间Y低于区间Y?我尝试使用ggExtra软件包[link] http://code.google.com/p/ggextra/进行对齐,但这并未解决问题。此外,我试图看看是否可以强制这两个地块的y轴的宽度相同,但没有运气。 – 2011-03-22 14:06:32
@Christian - 很高兴为你工作!我不确定我是否遵循你的后续问题。可能值得用一个可重现的例子来提出一个新问题。您将以这种方式获得更多的人流量,而在未来寻找类似答案的人将会遇到问题,而不是查看其他答案的评论。我不幸没有太多的'ggextra'的经验... – Chase 2011-03-22 14:15:09
谢谢!答案由@Andrie提供。我是Stack Overflow的新手,所以我不知道是否不恰当地提出一个关于“字面意义上”同一个问题的新问题。至少,我想你明白了我的观点:-) – 2011-03-22 14:45:38