[R] Multiple graphs
Dear R helpers, Last week Mr. Ista Zahn has taken lots of efforts and helped me with the following code generating basel_asset_wise (e.g. bank, corporate, sovereign) graphs. The code works excellently and assetwise grahs are aslo genereated. I have tried to customize the code as per my need. However, there is only one small problem with the graphs. The graph doesn't give me the asset names. I.e. in the graph which is meant for bank doesn't show me the name bank. Under the X - label i.e. Rating, it gives me number 1, Similarly for corporate and sovereign it gives the numbers 2 and 3 respectively. I understand it is like a spoon feeding, but I have really taken the efforts to find the solution and invested myself on whole of Saturday and Sunday and tried different options but just couldn't find the way. I will be grateful if someone guides me. I understand the default_probability should be between 0 and 1. However, just for disply checking purpose I have taken different figures. Regards Akshata # Here is the part of my code which generates the graphs - library(plyr) internal_scale = c("AAA","AA","A","BBB","BB","B","CCC") # internal_scale_id <- c(1:length(internal_scale)) # basel_asset_class <- c("Bank", "Corporate", "Sovereign") # basel_asset_class_id <- c(1:length(basel_asset_class)) output = data.frame(basel_asset_class_id = c(1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3), internal_scale_id = c(1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7), default_probability = c(10,28,32,44,57,62,78,14,23,38,47,56,65,74,1,9,16,25,36,49,64)) # # Generation of Graphs DP_graphs <- function(Data = output, class.var="basel_asset_class_id", ratings="internal_scale_id", IPD="default_probability") { class <- unique(as.character(Data[[class.var]]))[1] png(filename=sprintf("%s%s%s%s%s", "Curve ", "( ", class," )",".png"), width=480,height=480) par('bg'= "#CC") par(xaxt="n") plot(as.numeric(factor(Data[[ratings]])), Data[[IPD]], "b", ylab="PD", xlab="Rating", fg="#804000", font.main=2,cex.main=1,col="Red", col.main="black",col.axis="black" ,col.lab = "black") title("Exponential Curve", sub = paste("(", class,")"), cex.main=1.2, font.main= 2, col.main= "black", cex.sub = 1, font.sub = 2, col.sub ="black") par(xaxt="s") axis(1,at=1:length(unique(output$internal_scale_id)), labels = internal_scale) box(which="outer",bty = "o", col = "#804000") dev.off() } d_ply(output, .(basel_asset_class_id), DP_graphs) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to plot multiple graphs?
Dear sir, Thanks a lot for your great help. The graph is amazing. However, considering the number of classes (Here for an example purpose I have taken only 3 and in reality I will be dealing with even 25 classes), will it be possible (a) to generate separate graphs for each of these classes? (b) How do I draw a line connecting the plot points? Kindly advise. Thanks again sir. Regards Akshata On Tue, Mar 8, 2011 at 7:16 PM, John Kane wrote: > I renamed your dataset to df1 (df is a reserved word and changed the column > names to make typing easier. > > Using gggplot2 does this give you something like what you want? > = > library(ggplot2) > names(df1) <- c("class", "rate", "default") > > p <- ggplot(df1, aes(rate, default)) + geom_point() + facet_grid(class ~.) > p > ============== > > > > --- On Tue, 3/8/11, Akshata Rao wrote: > > > From: Akshata Rao > > Subject: [R] How to plot multiple graphs? > > To: r-help@r-project.org > > Received: Tuesday, March 8, 2011, 7:40 AM > > Dear R helpers, > > > > I have following data.frame giving asset class (i.e. bank, > > corporate, > > sovereign etc. as there could be number of classes) and > > rating-wise default > > frequency. > > > > I need to plot graphs for each of these classes i.e. bank, > > corporate and > > sovereign where I will be plotting ratings (AAA, AA, etc) > > on the x-axis and > > their respective default probabilities on Y-axis. > > > > I have tried to write a function as given below, but it > > produces only one > > graph and that too just a straight line. I have recently > > started my R > > venture and trying to learn through the help I receive from > > various R forum > > helpers and through old R mails which are achieved. > > > > My code is as given below. > > > > library(plyr) > > > > df = data.frame(basel_asset_class = > > > c("bank","bank","bank","bank","bank","bank","bank","corporate","corporate","corporate","corporate","corporate","corporate","corporate","sovereign","sovereign","sovereign","sovereign","sovereign","sovereign","sovereign"), > > ratings = > > > c("AAA","AA","A","BBB","BB","B","CCC","AAA","AA","A","BBB","BB","B","CCC","AAA","AA","A","BBB","BB","B","CCC"), > > default_probability = > > > c(0.0027,0.0029,0.0031,0.0034,0.0037,0.004,0.0043,0.0025,0.0024,0.0024,0.0023,0.0022,0.0021,0.0021,0.003,0.0031,0.0032,0.0033,0.0034,0.0035,0.0036)) > > > > n_name = as.character(unique(df$basel_asset_class)) > > > > IPD = df$default_probability > > > > internal_category = c(1:7) # Total > > types of ratings > > > > DP_yearly_graphs = function(class, ratings, IPD) > > > > { > > flname = paste("Exponential Curve For " , > > n_name) > > png(filename=sprintf("%s%s%s%s%s", "Curve ", > > "( ", n_name," )", > > ".png"),width=480,height=480) > > > > par('bg'= "#CC") > > par(xaxt="n") > > > > plot(internal_category, IPD, "b", ylab="PD", > > xlab="Rating", fg= > > "#804000", font.main=2,cex.main=1,col="Red",col.main= > > "black",col.axis="black" ,col.lab = "black") > > title("Exponential Curve", sub = paste("(", > > n_name,")"), cex.main = > > 1.2, font.main= 2, col.main= "black", > > cex.sub = 1, font.sub = 2, col.sub = > > "black") > > par(xaxt="s") > > axis(1,at=1:7) > > box(which="outer",bty = "o", col = > > "#804000") > > dev.off() > > > > } > > > > output_graph <- ddply(.data=ons1, .variables = > > "basel_asset_class", > > .fun=function(x) DP_yearly_graphs(class = x$n_name, > > ratings=x$ratings, IPD = x$IPD)) > > > > Don't understand where I am going wrong. Will be too > > grateful if someone can > > guide me through. > > > > Thanking in advance. > > > > Regards > > > > Akshata > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org > > mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, > > reproducible code. > > > > > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to plot multiple graphs?
Dear R helpers, I have following data.frame giving asset class (i.e. bank, corporate, sovereign etc. as there could be number of classes) and rating-wise default frequency. I need to plot graphs for each of these classes i.e. bank, corporate and sovereign where I will be plotting ratings (AAA, AA, etc) on the x-axis and their respective default probabilities on Y-axis. I have tried to write a function as given below, but it produces only one graph and that too just a straight line. I have recently started my R venture and trying to learn through the help I receive from various R forum helpers and through old R mails which are achieved. My code is as given below. library(plyr) df = data.frame(basel_asset_class = c("bank","bank","bank","bank","bank","bank","bank","corporate","corporate","corporate","corporate","corporate","corporate","corporate","sovereign","sovereign","sovereign","sovereign","sovereign","sovereign","sovereign"), ratings = c("AAA","AA","A","BBB","BB","B","CCC","AAA","AA","A","BBB","BB","B","CCC","AAA","AA","A","BBB","BB","B","CCC"), default_probability = c(0.0027,0.0029,0.0031,0.0034,0.0037,0.004,0.0043,0.0025,0.0024,0.0024,0.0023,0.0022,0.0021,0.0021,0.003,0.0031,0.0032,0.0033,0.0034,0.0035,0.0036)) n_name = as.character(unique(df$basel_asset_class)) IPD = df$default_probability internal_category = c(1:7) # Total types of ratings DP_yearly_graphs = function(class, ratings, IPD) { flname = paste("Exponential Curve For " , n_name) png(filename=sprintf("%s%s%s%s%s", "Curve ", "( ", n_name," )", ".png"),width=480,height=480) par('bg'= "#CC") par(xaxt="n") plot(internal_category, IPD, "b", ylab="PD", xlab="Rating", fg= "#804000", font.main=2,cex.main=1,col="Red",col.main= "black",col.axis="black" ,col.lab = "black") title("Exponential Curve", sub = paste("(", n_name,")"), cex.main = 1.2, font.main= 2, col.main= "black", cex.sub = 1, font.sub = 2, col.sub = "black") par(xaxt="s") axis(1,at=1:7) box(which="outer",bty = "o", col = "#804000") dev.off() } output_graph <- ddply(.data=ons1, .variables = "basel_asset_class", .fun=function(x) DP_yearly_graphs(class = x$n_name, ratings=x$ratings, IPD = x$IPD)) Don't understand where I am going wrong. Will be too grateful if someone can guide me through. Thanking in advance. Regards Akshata [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Applying function to multiple data
Dear R helpers, I know R language at a preliminary level. This is my first post to this R forum. I have recently learned the use of function and have been successful in writing few on my own. However I am not able to figure out how to apply the function to multiple sets of data. # MY QUERY Suppose I am having following data.frame df = data.frame(k = c(1:8), ratings = c("A", "B", "C", "D", "E", "F", "G", "H"), default_frequency = c(0.00229,0.01296,0.01794,0.04303,0.04641,0.06630,0.06862,0.06936)) # --- DP = function(k, ODF, ratings) { n <- length(ODF) tot_klnODF<- sum(k*log(ODF)) tot_k <- sum(k) tot_lnODF <- sum(log(ODF)) tot_k2 <- sum(k^2) slope<- exp((n * tot_klnODF - tot_k * tot_lnODF)/(n * tot_k2 - tot_k^2)) intercept <- exp((tot_lnODF - log(slope)* tot_k)/n) IPD <- intercept * slope^k return(data.frame(ratings = ratings, default_probability = round(IPD, digits = 4))) } result = DP(k = df$k, ODF = df$default_frequency, ratings = df$ratings) # The above code fetches me following result. However, I am dealing with only one set of data here as defined in 'df'. > result ratings default_probability 1 A 0.0061 2 B 0.0094 3 C 0.0145 4 D 0.0222 5 E 0.0342 6 F 0.0527 7 G 0.0810 8 H 0.1247 # MY PROBLEM Suppose I have data as given below Classk rating default_frequency Bank1 A0.00229 2 B 0.01296 3 C 0.01794 4 D 0.04303 5 E 0.04641 6 F 0.06630 7 G 0.06862 8 H 0.06936 Corporate1 A 0.00101 2 B 0.01433 3 C 0.02711 4 D 0.03701 5 E 0.04313 6 F 0.05600 7 G 0.06041 8 H 0.07112 Sovereign1 A 0.00210 2 B 0.01014 3 C 0.02001 4 D 0.04312 5 E 0.05114 6 F 0.06801 7 G 0.06997 8 H 0.07404 So I need to use the function "DP" defined above to generate three sets of results viz. for Bank, Corporate, Sovereign and save each of these results as diffrent csv files say as bank.csv, corporate.csv etc. Again please note that there could be say 'm' number of classes. I was trying to use the apply function but things are not working for me. I will really apprecaite the guidenace. I hope I am able to put up my query in a neat manner. Regards and thanking you all in advance. Akshata Rao [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.