ggplot2 output not showing in script

516 views
Skip to first unread message

Jahan

unread,
Jan 8, 2012, 3:54:09 PM1/8/12
to ggplot2
I was practicing R and ggplot so I made a fake data set and did some
calculations and graphs in a script. I have a combination of R's base
graphics and also ggplot's graphics. When I run the script--
source('myfile.R')--it skips over the boxplot, q-q plot, and all of
the ggplots. The graphs that do display are the histogram and the
plot(lmfit) What is happening here?

#create emr records data
emr=cbind(rep(factor('emr'),
50),rnorm(50,mean=250,sd=30),rnorm(50,mean=3,sd=1))
emr=data.frame(emr)
emr[,1]=factor('emr')
colnames(emr)=c('records','cost','numlabs')
#create paper records data
paper=cbind(rep(factor('paper'),
50),rnorm(50,mean=325,sd=30),rnorm(50,mean=6,sd=1))
paper=data.frame(paper)
paper[,1]=factor('paper')
colnames(paper)=colnames(emr)
#create mixed data
mixed=cbind(rep(factor('mixed'),
50),rnorm(50,mean=290,sd=15),rnorm(50,mean=4.6,sd=.5))
mixed=data.frame(mixed)
mixed[,1]=factor('mixed')
colnames(mixed)=colnames(emr)
#combine the three into one data frame and round the labs to integers
recordsdata=rbind(emr,paper)
recordsdata=rbind(recordsdata,mixed)
recordsdata$numlabs=round(recordsdata$numlabs)
print(recordsdata)
#linear model of records data
lmfit=lm(formula=recordsdata$cost~recordsdata$records)
#some useful plots
boxplot(recordsdata$cost~recordsdata$records)
qqnorm(emr$cost)
hist(emr$cost)
#pretty density plot
library(ggplot2)
qplot(recordsdata$cost)
ggplot(recordsdata,aes(cost,fill=records))
+geom_density(alpha=0.2)+xlim(140,400)
ggplot(recordsdata,aes(cost,fill=records))+geom_histogram()
plot(lmfit)

Jahan

unread,
Jan 8, 2012, 6:58:59 PM1/8/12
to ggplot2
I realized this text is very dense and hard to read so I separated
things out better. Here's a link to the script I made:
http://dl.dropbox.com/u/1852742/fakeemrdata.R
Thanks to Dennis Murphy for some tips on cleaning up the data creation
in the beginning. The changes are reflected here.

And here it is pasted:

#I created some fake EMR data with a function that generates random
data that
#is in roughly a normal distribution. I can set the approx. mean and
sd
#as well. The final data frame is called "recordsdata". I ran tests
and made
#plots and tables that will be useful. Note that if you were typing
these
#commands in R itself you don't have to use the print command.

#create emr records data
emr=data.frame(records=factor(rep('emr',50)),
cost=rnorm(50,mean=250,sd=30),
numlabs=rnorm(50,mean=3,sd=1))

#create paper records data
paper=data.frame(records=factor(rep('paper',50)),
cost=rnorm(50,mean=325,sd=30),
numlabs=rnorm(50,mean=6,sd=1))

#create mixed data
mixed=data.frame(records=factor(rep('mixed',50)),
cost=rnorm(50,mean=290,sd=15),
numlabs=rnorm(50,mean=4.6,sd=.5))

#combine the three into one data frame and round the labs to integers
recordsdata=rbind(emr,paper)
recordsdata=rbind(recordsdata,mixed)
recordsdata$numlabs=round(recordsdata$numlabs)
print(recordsdata)

#write the data frame to csv
#write.csv(recordsdata,file='foobar.csv')

#show summary by type of records
print(tapply(recordsdata$cost,recordsdata$records,summary))
print(tapply(recordsdata$cost,recordsdata$records,sd))

#two-way table of records means
recordstable=table(recordsdata$records,recordsdata
$cost>mean(recordsdata$cost))
print(recordstable)

#summary of table including chi-square
print(summary(recordstable))

#linear model of records data
lmfit=lm(formula=recordsdata$cost~recordsdata$records)
print(summary(lmfit))

#anova of lmfit
print(anova(lmfit))

#pair-wise t test of records data
print(pairwise.t.test(recordsdata$cost,recordsdata
$records,pool.sd=FALSE))

#some useful plots
boxplot(recordsdata$cost~recordsdata$records,ylab='Cost (Dollars)')
qqnorm(emr$cost)
hist(emr$cost)

#pretty ggplot plots
library(ggplot2)
qplot(recordsdata$cost)

#another
ggplot(recordsdata,aes(cost,fill=records))+geom_density(alpha=0.2)+
scale_x_continuous('Cost (Dollars)',limits=c(150,425))

#another
ggplot(recordsdata,aes(cost,fill=records))+geom_histogram()

#plots for the linear model
plot(lmfit)

Joshua Wiley

unread,
Jan 8, 2012, 7:04:21 PM1/8/12
to Jahan, ggplot2
In batch or script mode, you need to print() ggplot2 graphs. If you
wrap those in print calls, it should work.

cheers,

Josh

> --
> 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

--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

Reply all
Reply to author
Forward
0 new messages