2011-03-23 8 views
6

में टिक अंकों से कुछ टेक्स्ट अंक निकालें मैं बैंकिंग के संबंध में गैर-डिफॉल्टर्स और डिफॉल्टर्स पर कुछ शोध कर रहा हूं। उस संदर्भ में मैं बार प्लॉट में कुछ स्कोर के सापेक्ष अपने वितरण की साजिश रच रहा हूं। जितना अधिक स्कोर, क्रेडिट रेटिंग बेहतर होगी।ggplot bar plot

चूंकि डिफ़ॉल्ट की संख्या गैर-डिफ़ॉल्ट की साजिश की तुलना में बहुत सीमित है, उसी बार प्लॉट पर डिफ़ॉल्ट और गैर-डिफ़ॉल्ट पर प्लॉटिंग बहुत कम नहीं है क्योंकि आप शायद ही कभी डिफ़ॉल्ट देख सकते हैं। मैं फिर डिफॉल्टर्स के स्कोर के आधार पर दूसरी बार साजिश बना देता हूं, लेकिन डिफॉल्टर्स और गैर-डिफॉल्टर्स के दोनों स्कोरों की पूर्ण बार साजिश के समान अंतराल पैमाने पर। मैं फिर पहली बार साजिश में ऊर्ध्वाधर रेखाएं जोड़ता हूं जो दर्शाता है कि उच्चतम और निम्नतम डिफॉल्टर स्कोर कहां स्थित है। यह देखने के लिए कि डिफॉल्टर्स का वितरण दोनों डिफॉल्टर्स और गैर-डिफॉल्टर्स के समग्र वितरण में फिट है।

एक्स-अक्ष आसानी से बहुत "भीड़" हो जाता है। मैं टिकों के लिए कुछ पाठ को हटाना चाहता हूं, लेकिन सभी टिक अंक नहीं।

नीचे कोड है जिसका उपयोग मैं प्रतिस्थापित (बीजित) यादृच्छिक डेटा के साथ बदल रहा हूं।

पहला बार प्लॉट वह है जो मैं टिक अंकों पर पाठ के संबंध में रखना चाहता हूं, लेकिन मुझे दूसरी बार साजिश में मेरे सभी टिक्स याद आ रहे हैं। दूसरी बार साजिश में "भीड़" -नेस दिखाया गया है!

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) 

किसी भी मदद की बहुत सराहना की जाती है!

धन्यवाद,

ईसाई

उत्तर

3

अगर मैं सही ढंग से समझ, तो आप बस हर दूसरे लेबल के लिए खाली पाठ लेबल निर्दिष्ट कर सकते हैं,

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

+0

मीठा वैकल्पिक हल !! आपका बहुत बहुत धन्यवाद! –

+0

'ggplot2 :: interleave' वर्तमान ggplot2 लाइब्रेरी (संस्करण 2.0.0) में नहीं मिला है। इस फ़ंक्शन का विकल्प क्या है? – user890739

+0

@ user890739 इंटरलीव फ़ंक्शन, 'ggplot2 ::: interleave' तक पहुंचने के लिए आपको एक तिहाई कोलन की आवश्यकता होगी। हालांकि, अपने कार्यों पर ट्रिपल कॉलन का उपयोग करना याद रखें, क्योंकि ये फ़ंक्शंस पैकेज में आंतरिक बने रहने के लिए हैं; देखें: https://stat.ethz.ch/R-manual/R-devel/library/base/html/ns-dblcolon.html – aseagram

0

यहाँ एक और संस्करण, आधारित है पर @baptiste, जो कि यहां तक ​​कि अजीब, हर तीसरे, आदि के आसान चयन की अनुमति देता है

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 
) 

enter image description here