R code to mimic SAS Macro - ggplot geom_bar

121 views
Skip to first unread message

pradip...@samhsa.hhs.gov

unread,
Aug 10, 2011, 12:13:39 PM8/10/11
to ggplot2, pradip...@samhsa.hhs.gov
Hello,

I am a new R users. Mimicing the SAS macro, I have written the
following R function to produce pdf output of several bar plots using
ggplot geom_bar from one run.

I am getting the following error message:
an error Error in eval(expr, envir, enclos) : object 'yvar' not found

I would appreciate receiving your help debug the program.

Thanks,

Pradip


#### The R Script begins here ####
setwd ("F:/Disorders")
library (ggplot2)

myPlot <- function(lFrame,yTrigger )
{
yvar = rep(c(0),21)
yvar_l = rep(c(0),21)
yvar_u = rep(c(0),21)
outFile = "default.pdf"


if(yTrigger == 1)
{
yvar = lFrame$abodill
yvar_l = lFrame$abodill_l
yvar_u = lFrame$abodill_u
outFile = "abodill_age3.pdf"


}
else if (yTrigger == 2)
{
yvar = lFrame$abodmrj
yvar_l = lFrame$abodmrj_l
yvar_u = lFrame$abodmrj_u
OutFile = "abodmrj_age3.pdf"


}

lFrame$age <- factor(lFrame$age,
labels=c("12-17", "18-25", "26 or Older")
)

# In the data frame, race/ethnicity is coded as follows:
# 1=White, 2=Black, 3=AI/AN, 4="Hawiian/OPI",
# 5=Asian, 6=Multi Races,7=Hispanic

lFrame$race <- factor(
lFrame$race,
levels=c
(5,2,4,7,1,6,3),
labels=c("Asian","Black","Hawiian/OPI",
"Hispanic","White",
"Multi Races","AI/
AN") )

limits <- aes(ymax = yvar_u, ymin=yvar_l)
print("limits")
dodge <- position_dodge(width=1)

p <- ggplot(lFrame, aes(y=yvar, x=race, fill=race ))+
geom_bar(position=dodge) +
geom_errorbar(limits, position=dodge, width=0.3) +
#geom_text (aes(label = abodmrj,colour="black", face="bold"))
+
facet_wrap(~ age, ncol=3)+
scale_fill_manual(values=c("brown", "grey", "red",
"lightgreen",
"yellow2", "violetred1", "sienna2")
)+
xlab("")+
ylab("Percentages")+

opts(legend.position="none",
panel.grid.major=theme_blank(),
panel.grid.minor=theme_blank(),
title="Past-Year Marijuana Use Disorder (2002-2010)",
axis.text.y=theme_text(size=11, face="bold"),
axis.text.x=theme_text(angle=90, hjust=0, size=15,
face="bold"),
panel.background=theme_rect(),
plot.title = theme_text(size=20, face="bold")
)

print (p)
ggsave(file=outFile, width=12, height=8)


}
load("rdframe1.rdata")

myPlot(rdframe1, 1)
myPlot(rdframe1, 2)


### The R Script ends here #####

I

Luciano Selzer

unread,
Aug 10, 2011, 2:22:21 PM8/10/11
to pradip...@samhsa.hhs.gov, ggplot2
Could you please provide us with some data to play with? It's hard to debug it without it.

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Luciano Selzer

unread,
Aug 10, 2011, 3:06:12 PM8/10/11
to Muhuri, Pradip (SAMHSA/CBHSQ), ggplot2
Pradip, the issue was that the vars you were referencing weren't in the data.frame. Below is the corrected code, the part that needs to be changed.

myPlot <- function(lFrame,yTrigger ) {
 lFrame$yvar = rep(c(0),21)
 lFrame$yvar_l = rep(c(0),21)
 lFrame$yvar_u = rep(c(0),21)
 lFrame$outFile = "default.pdf"
 if(yTrigger == 1) {
   lFrame$yvar = lFrame$abodill
   lFrame$yvar_l = lFrame$abodill_l
   lFrame$yvar_u = lFrame$abodill_u
   outFile = "abodill_age3.pdf"
 } else if (yTrigger == 2) {
    lFrame$yvar = lFrame$abodmrj
    lFrame$yvar_l = lFrame$abodmrj_l
    lFrame$yvar_u = lFrame$abodmrj_u
    lFrame$OutFile = "abodmrj_age3.pdf"
 }
Luciano


2011/8/10 Muhuri, Pradip (SAMHSA/CBHSQ) <Pradip...@samhsa.hhs.gov>

Dear Luciano,

 

Thank you so much for your willingness to help. Attached please find the original R script (that just worked fine for one graph) and the R data for you to play with.

 

I look forward to hearing from you.

 

Pradip

 

Pradip K. Muhuri, PhD

Statistician

Substance Abuse & Mental Health Services Administration

The Center for Behavioral Health Statistics and Quality

Division of Population Surveys

1 Choke Cherry Road, Room 7-1023

Rockville, MD 20857

 

Tel: 240-276-1070

Fax: 240-276-1260

e-mail: Pradip...@samhsa.hhs.gov

 

The Center for Behavioral Health Statistics and Quality your feedback.  Please click on the following link to complete a brief customer survey:   http://cbhsqsurvey.samhsa.gov

age3_geombar_facet_ill.R
rdframe1.rdata

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Aug 10, 2011, 3:31:46 PM8/10/11
to Luciano Selzer, ggplot2

Luciano,

 

Thank you so much for your help.  A huge improvement – I now get the first plot.

 

Still there are some issues.

1)      No title for the first plot

2)      X-axis labels are displaced for the first plot

3)  No output for the second plot, with the following error message: Error in strsplit(filename, "\\.") : object 'outFile' not found

 

Could you please help resolve these issues?

 

Regards,

abodill_age3.pdf
New_RScript_facet_ill.R

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Aug 10, 2011, 4:09:51 PM8/10/11
to Matthew Krachey, Luciano Selzer, ggplot2

Mathew and Luciano,

 

I have also noticed that problem, which I have fixed.  Now I get both the plots.  The only issue is that I am not getting the title in either of these two plots. 

 

Thanks,

 

Pradip K. Muhuri, PhD

Statistician

Substance Abuse & Mental Health Services Administration

The Center for Behavioral Health Statistics and Quality

Division of Population Surveys

1 Choke Cherry Road, Room 7-1023

Rockville, MD 20857

 

Tel: 240-276-1070

Fax: 240-276-1260

e-mail: Pradip...@samhsa.hhs.gov

 

The Center for Behavioral Health Statistics and Quality your feedback.  Please click on the following link to complete a brief customer survey:   http://cbhsqsurvey.samhsa.gov

From: Matthew Krachey [mailto:matthew...@yahoo.com]
Sent: Wednesday, August 10, 2011 4:04 PM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Subject: Re: R code to mimic SAS Macro - ggplot geom_bar

 

For point 3) there is a typo for yTrigger==2, it should be outFile, not OutFile

 

 

<abodill_age3.pdf><New_RScript_facet_ill.R>

 

New_RScript_facet_ill.R
Reply all
Reply to author
Forward
0 new messages