gpt4 book ai didi

从ggplot条形图中的刻度线中删除一些文本标记

转载 作者:行者123 更新时间:2023-12-04 07:44:20 26 4
gpt4 key购买 nike

我正在对非违约者和银行违约者进行一些研究。在这种情况下,我正在用条形图绘制它们相对于某些得分的分布。分数越高,信用等级越好。

由于与非默认值的数量相比,默认值的数量非常有限,因此在同一条形图上绘制默认值和非默认值的效果不是很好,因为您几乎看不到默认值。然后,我仅基于违约者的得分来制作第二个柱状图,但间隔与默认者和非违约者的得分的完整柱状图相同。然后,我在第一个条形图上添加垂直线,以指示最高和最低默认值得分所在的位置。这是为了了解违约者的分布在违约者和非违约者的总体分布中所处的位置。

x轴很容易变得“拥挤”。我想删除一些刻度线的文本,但不是全部的刻度线。

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

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

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)

很感谢任何形式的帮助!

谢谢,

基督教

最佳答案

如果我理解正确,则只需为其他所有标签指定空白文本标签,

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)

enter image description here

关于从ggplot条形图中的刻度线中删除一些文本标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5407367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com