Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd vertical lines to ggplot2 bar plot
    primarykey
    data
    text
    <p>I am doing some research on non-defaulters and defaulters with regards to banking. In that context I am plotting their distributions relative to some score in a bar plot. The higher the score, the better the credit rating. </p> <p>Since the number of defaults is very limited compared to the number of non-defaults plotting the defaults and non-defaults on the same bar plot is not very giving as you hardly can see the defaults. I then make a second bar plot based on the defaulters' scores only, but on the same interval scale as the full bar plot of both the scores of the defaulters and non-defaulters. I would then like to add vertical lines to the first bar plot indicating where the highest defaulter score is located and the lowest defaulter score is located. That is to get a view of where the distribution of the defaulters fit into that of the overall distribution of both defaulters and non-defaulters. </p> <p>Below is the code I am using replaced with (seeded) random data instead.</p> <pre><code>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&lt;-rnorm(10000,sd=1)-2 DS&lt;-rnorm(100,sd=2)-5 #Cutoffs are constructed such that intervals of size 0.3 #contain all values of NDS &amp; DS minCutoff&lt;--9.3 maxCutoff&lt;-2.1 #Generate the actual interval "bins" NDS_CUT&lt;-cut(NDS,breaks=seq(minCutoff, maxCutoff, by = 0.3)) DS_CUT&lt;-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&lt;-levels(cut(NDS,breaks=seq(minCutoff, maxCutoff, by = 0.3)))[1] maxDS_bar&lt;-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&lt;-cbind(as.data.frame(NDS_CUT),rep(factor("State-1"),length(NDS_CUT))) colnames(NDSdataframe)&lt;-c("Score","Action") DSdataframe&lt;-cbind(as.data.frame(DS_CUT),rep(factor("State-2"),length(DS_CUT))) colnames(DSdataframe)&lt;-c("Score","Action") fulldataframe&lt;-rbind(NDSdataframe,DSdataframe) attach(fulldataframe) #Plot the full distribution of NDS &amp; DS # with geom_vline(xintercept = minDS_bar) + geom_vline(xintercept = maxDS_bar) # that unfortunately does not show :-( fullplot&lt;-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&lt;-cbind(as.data.frame(DS_CUT),rep(factor("State-2"),length(DS_CUT))) colnames(DSdataframe2)&lt;-c("theScore","theAction") #Calucate max number of observations to adjust bar plot of DS only myMax&lt;-max(table(DSdataframe2))+1 attach(DSdataframe2) #Generate bar plot of DS only subplot&lt;-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 &lt;- 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) </code></pre> <p>Furthermore, if anybody has an idea of how I can align the to plot so that correct intervals are just below/above each other on the grid plot</p> <p>Hope somebody is able to help!</p> <p>Thanks in advance,</p> <p>Christian</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload