[R] How to plot multiple graphs?
Ista Zahn
izahn at psych.rochester.edu
Tue Mar 8 17:52:13 CET 2011
Hi Akshata,
This is really a mess, but I'll try to "guide you through" (see below).
On Tue, Mar 8, 2011 at 7:40 AM, Akshata Rao <akshata.rao1908 at gmail.com> wrote:
> 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.
OK, good description of the goal.
>
> 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))
>
Good, thanks for including the example data set.
> n_name = as.character(unique(df$basel_asset_class))
>
> IPD = df$default_probability
Why do you copy this information to IPD? You don't really need to
copies of it, and it just makes things more confusing.
>
> 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'= "#FFFFCC")
> 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()
>
> }
>
Several problems here:
1) You never use the variables "class" or "ratings" in the function at
all. You do use IPD, but that seems to be incidental rather than
intentional.
2) You also initialize "flname" inside your function, but never use it.
3) I'm not sure the plotting command do what you think they do. I
never learned base graphics so I'm not going to be much help with this
one -- I'm just noticing the you labeled the graphes "Exponential
curve", but that doesn't seem descriptive of the actual result.
You should consult a manual on writing R functions as well as basic
'intro to R" kinds of tutorials. (see the official and contributed
documentation on CRAN).
> 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))
There are several problems with this as well:
1) ddply is the wrong tool for the job. ddply takes a data.frame as
input, and returns a data.frame as output. You don't want a data.frame
as output, you want figures. The proper way to do this with plyr is
with the d_ply function which returns nothing. See ?ddply and ?d_ply
2) What is ons1? You never define it! I guess you want .data=df
3) within the ddply call x refers to the subset of .data based on
.variables. If .data = df, there is no n_name in x. Same goes for
x$IPD.
OK, so here is a fixed version. Note that we don't need to make
separate IPD, n_name, or internal_category variables.
# Get rid of clutter:
rm(list=c("df", "internal_category", "IPD", "n_name"))
# name the data dat instead of df
dat <- structure(list(basel_asset_class = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("bank", "corporate", "sovereign"), class = "factor"),
ratings = structure(c(3L, 2L, 1L, 6L, 5L, 4L, 7L, 3L, 2L,
1L, 6L, 5L, 4L, 7L, 3L, 2L, 1L, 6L, 5L, 4L, 7L), .Label = c("A",
"AA", "AAA", "B", "BB", "BBB", "CCC"), class = "factor"),
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
)), .Names = c("basel_asset_class", "ratings", "default_probability"
), row.names = c(NA, -21L), class = "data.frame")
# Fix the function
DP_yearly_graphs <- function(Data=dat, class.var="basel_asset_class",
ratings="ratings", 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'= "#FFFFCC")
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:7)
box(which="outer",bty = "o", col = "#804000")
dev.off()
}
# Make the graphs
d_ply(dat, .(basel_asset_class), DP_yearly_graphs) # I prefer this
alternate *ply syntax, but up to you.
HTH,
Ista
>
> 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 at 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.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
More information about the R-help
mailing list