首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从绘图条形图中的刻度线中删除几个文本标记

从绘图条形图中的刻度线中删除几个文本标记
EN

Stack Overflow用户
提问于 2011-03-23 23:11:03
回答 2查看 7.2K关注 0票数 10

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

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

X轴很容易变得非常“拥挤”。我想删除一些刻度的文本,但不是所有刻度。

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

第一个条形图是我想要的关于刻度线上的文本的条形图,但我错过了第二个条形图中的所有刻度。在第二个条形图中,显示了“拥挤的”-ness!

代码语言:javascript
复制
library(ggplot2)
library(ggExtra)

#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_vlines

#Get the tick texts I want to show
myLevels<-levels(cut(NDS,breaks=seq(roundDownNDS, roundUpNDS, by = 0.3)))
lengthMyLevels<-length(myLevels)
myBreaks<-seq(1,lengthMyLevels,3)
chosenbreaks<-myLevels[myBreaks[1]]
for(i in 2:length(myBreaks))
 {
 chosenbreaks<-rbind(chosenbreaks,myLevels[myBreaks[i]])
 }


#Generate the plot of both NDS & DS
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,size=8))  + opts(legend.position = "none")    + xlab("Scoreinterval") + ylab("Antal pr. interval") + geom_vline(aes(xintercept = minDS_bar, colour="red")) + geom_vline(aes(xintercept = maxDS_bar, colour="red")) + scale_x_discrete("test",breaks=chosenbreaks)

#Generate second dataframe for the plot of DS only
DSdataframe2<-cbind(na.omit(as.data.frame(DS_CUT)),rep(factor("Fallit"),length    (na.omit(as.data.frame(DS_CUT)))))
colnames(DSdataframe2)<-c("theScore","theAction")

#Calculate max value for the DS
myMax<-max(table(DSdataframe2))+1

attach(DSdataframe2)

#Generate plot for the 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")

#Using the ggExtra package the to plots are aligned
align.plots(fullplot, subplot)

detach(DSdataframe2)
detach(fulldataframe)

任何帮助都是非常感谢的!

谢谢,

克里斯蒂安

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-24 16:40:09

如果我理解正确的话,您可以简单地为其他标签指定空文本标签,

代码语言:javascript
复制
library(ggplot2)

interleave <- function(x,y){
  lx <- length(x)
  ly <- length(y)
  n <- max(lx,ly)
  as.vector(rbind(rep(x, length.out=n), rep(y, length.out=n)))
}

d <- data.frame(x=1:10, y=rnorm(10))

my_breaks <- seq(1,10,by=1)
my_labs <- interleave(seq(1,10,by=2), "")

qplot(x,y,data=d)+
  scale_x_continuous(breaks=my_breaks, labels=my_labs)

票数 9
EN

Stack Overflow用户

发布于 2017-02-04 16:42:33

这是另一个版本,基于@baptiste,允许轻松选择偶数或奇数,每三个等等。

代码语言:javascript
复制
library(ggplot2)
library(gridExtra)

## helper function
## periodically replace orig with .fill
label_fill <- function(orig, .offset=0, .mod=2, .fill=""){
    ## replace
    ii <- as.logical(
        ## offset==0 keeps first
        (1:length(orig)-1+.offset) %% .mod
    )
    orig[ii] <- .fill
    orig
}

## data, labels
nn <- 10
my_dat <- data.frame(x=1:nn, y=rnorm(nn))
my_breaks <- my_dat$x

my_plot <- (
    ggplot(my_dat, aes(x,y))
    + geom_line()
    ## default: every other, start from 1
    ## by default, function takes breaks
    + scale_x_continuous(
        breaks=my_breaks, labels=label_fill
    )
    + theme_bw()
)

## another form 
## manually pass breaks
every_third <- scale_x_continuous(
    breaks=my_breaks, 
    labels=label_fill(my_breaks, .mod=3)
)

## side-by-side
grid.arrange(ncol=2,
    my_plot, 
    ## every third with offset
    my_plot + every_third
)

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

https://stackoverflow.com/questions/5407367

复制
相关文章

相似问题

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