Error in eval(expr, envir, enclos) : object 'dx' not found

2,839 views
Skip to first unread message

pradip...@samhsa.hhs.gov

unread,
Jan 26, 2013, 10:26:23 AM1/26/13
to ggp...@googlegroups.com
Hello,

Using a reproducible example (appended below), my goal is to write a function that can be be used to draw several ggplot-generated graphs one after another. But, this test program only involves producing one graph for testing purposes.

I am getting an error. I searched stack oveflow for tips and spent hours, but failed to resolve the issue. 

Your help would be highly appreciated.

regards, and thanks in advance.

Pradip Muhuri

Below is my copy/paste of the console.

####### console followed by a complete reproducible example with data and code #########

Error in eval(expr, envir, enclos) : object 'dx' not found

plotfunc <- function(dx, dy, dxlab, dylab, outFile) {
+     p1 <- ggplot ( dat, aes(x= dx, y=dy, colour=mm.state) )+
+     geom_text(aes(label=stcode), size=8, vjust=0)+
+     xlab(dxlab)+
+     ylab(dylab)+
+     theme(text = element_text(size=25,face = "bold"),
+           axis.text.x = element_text(hjust=1, face="bold"),
+           axis.text.y = element_text(hjust=1, face="bold"),
+           axis.title.y = element_text(face="bold"),
+           axis.title.x = element_text( vjust=1, face="bold"),
+           legend.position="none" ) 
+     print (p1)
+     ggsave(file=outFile, width=15, height=7)
+ }
> 
> 
> plotfunc(dat$obtmj_p, dat$prevmj_p,
+          "Fairly/Very Easy to Obtain Marijuana","Past Year Marijuana Use","MrjUseAccess.png")
Error in eval(expr, envir, enclos) : object 'dx' not found


###### The Reproducible Example Begins Here

setwd ("E:/CSAP_P1")
dat <- read.csv (text=
"id,stcode,mm.state,obtmj_p,prevmj_p
Alabama,AL,no,49.6,11.5
Alaska,AK,yes,55,16.7
Arizona,AZ,yes,52.5,16.2
Arkansas,AR,no,50.5,13.3
California,CA,yes,51.1,13.6
Colorado,CO,yes,55.1,17.8
Connecticut,CT,yes,56.3,17.2
Delaware,DE,yes,53.6,14.5
District of Columbia,DC,yes,53.5,14.2
Florida,FL,yes,52.7,14
Georgia,GA,no,52.5,12.1
Hawaii,HI,yes,49.4,17.1
Idaho,ID,no,48.3,12.8
Illinois,IL,no,52.7,13.1
Indiana,IN,no,49.6,12.8
Iowa,IA,no,46.3,11.5
Kansas,KS,no,44.3,12.8
Kentucky,KY,no,52.9,14.2
Louisiana,LA,no,49.7,11.3
Maine,ME,yes,55.6,17.9
Maryland,MD,no,53.9,14
Massachusetts,MA,yes,55.4,18.3
Michigan,MI,yes,52.4,14.9
Minnesota,MN,no,51.5,14.8
Mississippi,MS,no,43.2,9.2
Missouri,MO,no,48.7,14.4
Montana,MT,yes,56.4,18.8
Nebraska,NE,no,45.7,12.6
Nevada,NV,yes,54.2,16.8
New Hampshire,NH,no,56.1,16.3
New Jersey,NJ,yes,53.2,12.2
New Mexico,NM,yes,57.6,19
New York,NY,no,53.7,15.1
North Carolina,NC,no,52.2,14.8
North Dakota,ND,no,48.6,13.5
Ohio,OH,no,50.9,14.2
Oklahoma,OK,no,47.2,12.9
Oregon,OR,yes,54,17.6
Pennsylvania,PA,no,53,14.1
Rhode Island,RI,yes,57.2,18.6
South Carolina,SC,no,50.5,11.9
South Dakota,SD,no,43.4,14.9
Tennessee,TN,no,48.9,11.7
Texas,TX,no,48.7,11.9
Utah,UT,no,42,9.6
Vermont,VT,yes,58.7,19.6
Virginia,VA,no,51.8,14.2
Washington,WA,yes,53.5,14.5
West Virginia,WV,no,52.8,14.3
Wisconsin,WI,no,49.9,15.7
Wyoming,WY,no,49.2,15.1"
, sep=',', dec='.', colClasses= c("character", "character", "factor", "numeric", "numeric"), header=TRUE)

str(dat)

library (ggplot2)
loadedNamespaces()

  plotfunc <- function(dx, dy, dxlab, dylab, outFile) {
    p1 <- ggplot ( dat, aes(x= dx, y=dy, colour=mm.state) )+
    geom_text(aes(label=stcode), size=8, vjust=0)+
    xlab(dxlab)+
    ylab(dylab)+
    theme(text = element_text(size=25,face = "bold"),
          axis.text.x = element_text(hjust=1, face="bold"),
          axis.text.y = element_text(hjust=1, face="bold"),
          axis.title.y = element_text(face="bold"),
          axis.title.x = element_text( vjust=1, face="bold"),
          legend.position="none" )
    print (p1)
    ggsave(file=outFile, width=15, height=7)
}


plotfunc(dat$obtmj_p, dat$prevmj_p,
         "Fairly/Very Easy to Obtain Marijuana","Past Year Marijuana Use","MrjUseAccess.png")

Brandon Hurr

unread,
Jan 26, 2013, 10:41:46 AM1/26/13
to pradip...@samhsa.hhs.gov, ggplot2




--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
 
 

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Jan 26, 2013, 11:57:21 AM1/26/13
to Brandon Hurr, ggplot2, Muhuri, Pradip (SAMHSA/CBHSQ)
Hi Brandon,

Thank so much for the tips. I am close to resolving the issue. But, there is still an issue (error following ggsave ()).  Please see below.

regards,

Pradip

# I have added to the first line: , environment = environment()


 plotfunc <- function(dx, dy, dxlab, dylab, outFile) {
    p1 <- ggplot ( dat, aes(x= dx, y=dy, colour=mm.state), environment = environment() )+

    geom_text(aes(label=stcode), size=8, vjust=0)+
    xlab(dxlab)+
    ylab(dylab)+
    theme(text = element_text(size=25,face = "bold"),
          axis.text.x = element_text(hjust=1, face="bold"),
          axis.text.y = element_text(hjust=1, face="bold"),
          axis.title.y = element_text(face="bold"),
          axis.title.x = element_text( vjust=1, face="bold"),
          legend.position="none" )
    print (p1)
    ggsave(file=outFile, width=15, height=7)
}


plotfunc(dat$obtmj_p, dat$prevmj_p,
         "Fairly/Very Easy to Obtain Marijuana","Past Year Marijuana Use","MrjUseAccess.png")


1. Adding environment = environment()  gives me the desired chart.  Now I get a new error,which, I believe, has to do with the ggsave ().  


Error in grDevices::png(..., width = width, height = height, res = dpi,  : 
  unable to start png() device
In addition: Warning messages:
1: In grDevices::png(..., width = width, height = height, res = dpi,  :
  Unable to open file 'MrjUseAccess.png' for writing
2: In grDevices::png(..., width = width, height = height, res = dpi,  :
  opening device failed

2. Alternatively, the use of environment=parent.env() gives me the following error:
 p1 <- ggplot ( dat, aes(x= dx, y=dy, colour=mm.state), environment = parent.env() )+


Error in parent.env() : argument "env" is missing, with no default


I am looking forward to your advice.

Thanks and regards,

Pradip



From: Brandon Hurr [brando...@gmail.com]
Sent: Saturday, January 26, 2013 10:41 AM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Cc: ggplot2
Subject: Re: Error in eval(expr, envir, enclos) : object 'dx' not found

Dennis Murphy

unread,
Jan 26, 2013, 4:01:18 PM1/26/13
to Muhuri, Pradip (SAMHSA/CBHSQ), ggplot2
This works for me:

plotfunc <- function(dat, dxlab, dylab, outFile) {
require(ggplot2)
require(grid)
p1 <- ggplot ( dat, aes(x= obtmj_p, y=prevmj_p, colour=mm.state) )+
geom_text(aes(label=stcode), size=8, vjust=0)+
xlab(dxlab)+
ylab(dylab)+
theme(text = element_text(size=25,face = "bold"),
axis.text.x = element_text(hjust=1, face="bold"),
axis.text.y = element_text(hjust=1, face="bold"),
axis.title.y = element_text(face="bold", vjust = 0.2),
axis.title.x = element_text(vjust = 0, face="bold"),
plot.margin = unit(c(1, 1, 2, 2), "lines"),
legend.position="none" )
print (p1)
ggsave(file=outFile, width=15, height=7)
}

plotfunc(dat,
dxlab = "Fairly/Very Easy to Obtain Marijuana",
dylab = "Past Year Marijuana Use",
outFile = "pradip.png")

I expanded the plot margins a bit to accommodate the extra size of the
labels and played around with vjust a bit in the y-direction to
separate it somewhat from the axis tick labels.

Note: If you know which variables are to be plotted, and they're
routinely the same ones across different data sets, then you should
use the data frame as the input argument and define the variable roles
inside aes() as shown above. If the variables are going to change as
well as the data sets, then you should have arguments for both the
data frame and variables and use aes_string() inside the function
instead, something like

plotfunc2 <- function(dat, xvar = NULL, yvar = NULL, dxlab, dylab, outFile) {
require(ggplot2)
require(grid)
p1 <- ggplot ( dat, aes_string(x= xvar, y=yvar, colour="mm.state") )+
geom_text(aes(label=stcode), size=8, vjust=0)+
xlab(dxlab)+
ylab(dylab)+
theme(text = element_text(size=25,face = "bold"),
axis.text.x = element_text(hjust=1, face="bold"),
axis.text.y = element_text(hjust=1, face="bold"),
axis.title.y = element_text(face="bold", vjust = 0.2),
axis.title.x = element_text(vjust = 0, face="bold"),
plot.margin = unit(c(1, 1, 2, 2), "lines"),
legend.position="none" )
print (p1)
ggsave(file=outFile, width=15, height=7)
}

plotfunc2(dat,
xvar = "obtmj_p",
yvar = "prevmj_p",
dxlab = "Fairly/Very Easy to Obtain Marijuana",
dylab = "Past Year Marijuana Use",
outFile = "pradip2.png")

The variables need to be quoted in the call; otherwise R will look for
an object named 'obtmj_p' in the calling environment.

Dennis

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Jan 26, 2013, 8:02:31 PM1/26/13
to Dennis Murphy, ggplot2
Dear Dennis,

Thank you so much for coming up with the alternative solutions and explaining the code to me.

Your method 2 is the one I am interested in. My data set will remain the same but the variable will change.

It seems that if I use the aes_string () I don't need to use the environment=environment as I have used in my following revised code (also ran successfully) with tips and resources sent by Brandon.

regards,

Pradip Muhuri

############

plotfunc <- function(dx, dy, dxlab, dylab, outFile) {

p1 <- ggplot ( data, aes(x= dx, y=dy, colour=mm.state), environment=environment () )+


geom_text(aes(label=stcode), size=8, vjust=0)+
xlab(dxlab)+
ylab(dylab)+
theme(text = element_text(size=25,face = "bold"),
axis.text.x = element_text(hjust=1, face="bold"),
axis.text.y = element_text(hjust=1, face="bold"),
axis.title.y = element_text(face="bold"),
axis.title.x = element_text( vjust=1, face="bold"),
legend.position="none" )
print (p1)

ggsave(file=outFile, width=11, height=7)
}

plotfunc(data$obtmj_p, data$prevmj_p,


"Fairly/Very Easy to Obtain Marijuana",
"Past Year Marijuana Use",
'MrjUseAccess.png')

________________________________________
From: Dennis Murphy [djm...@gmail.com]
Sent: Saturday, January 26, 2013 4:01 PM

Reply all
Reply to author
Forward
0 new messages