首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向ggplot2条形图添加垂直线

向ggplot2条形图添加垂直线
EN

Stack Overflow用户
提问于 2011-03-22 21:47:47
回答 2查看 4.7K关注 0票数 1

我正在做一些关于银行方面的非违约者和违约者的研究。在这种情况下,我在柱状图中绘制了它们相对于某个分数的分布。分数越高,信用评级越好。

由于缺省值的数量与非缺省值的数量相比非常有限,因此在同一条形图上绘制缺省值和非缺省值并不是很有意义,因为您几乎看不到默认值。然后,我只根据违约者的分数制作第二个条形图,但间隔比例与违约者和非违约者的分数的完整条形图相同。然后,我想在第一个条形图中添加垂直线,指示最高违约者得分和最低违约者得分所在的位置。这是为了了解违约者的分布与违约者和非违约者的总体分布相适应的情况。

下面是我用(种子)随机数据替换的代码。

代码语言:javascript
复制
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)

此外,如果有人知道如何对齐to图,以便在网格图上正确的间隔恰好低于/高于其他间隔

希望有人能帮上忙!

提前谢谢你,

克里斯蒂安

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-22 21:54:49

aes包装在geom_vline层中的xintercept周围:

代码语言:javascript
复制
... + geom_vline(aes(xintercept = minDS_bar)) + geom_vline(aes(xintercept = maxDS_bar)) 
票数 3
EN

Stack Overflow用户

发布于 2011-03-22 22:15:08

问题1:

由于您提供了垂直线作为数据,因此必须首先使用aes()映射美学

代码语言:javascript
复制
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(aes(xintercept = minDS_bar)) + 
        geom_vline(aes(xintercept = maxDS_bar)) 

第二个问题:

要对齐图,可以使用ggExtra包中的align.plots()函数

代码语言:javascript
复制
install.packages("dichromat")
install.packages("ggExtra", repos="http://R-Forge.R-project.org")
library(ggExtra)

ggExtra::align.plots(fullplot, subplot)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5392275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档