0

I am trying to plot a scatterplot and box plot of two continuous varaibles but am getting an error that says,

Warning message: In par(fig = c(0, 0.8, 0.55, 1), new = TRUE) : calling par(new=TRUE) with no plot

The code worked when I simply replaced lines 4-6 of my code with:

plot(mydata$gre, mydata$gpa, xlab="GRE",ylab="GPA")

Here's my code:

mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
par(fig=c(0,0.8,0,0.8), new=TRUE)
#plot(mydata$gre, mydata$gpa, xlab="GRE",ylab="GPA")
d<-ggplot(mydata,aes(x=mydata$gre, y=mydata$gpa))
d<-d+geom_line()
d
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(mydata$gre, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mydata$gpa, axes=FALSE)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)

Could you please shed some light on what I am doing wrong with regards to ggplot since R isn't recognizing it? What's really weird is that when I type d, the name of my ggplot, I get a plot...

user3780173
  • 153
  • 1
  • 2
  • 7
  • 1) A warning message is not an error. 2) Did you read the warning message? It's pretty clear. – Señor O Jun 27 '14 at 18:16
  • remove the hashtag before plot. will work then. – jalapic Jun 27 '14 at 18:17
  • I have read the warning message and don't understand why there is no plot – user3780173 Jun 27 '14 at 18:18
  • Uh... because you're not plotting anything – Señor O Jun 27 '14 at 18:19
  • Shouldn't printing d (line 6) create a plot? – user3780173 Jun 27 '14 at 18:20
  • `ggplot` uses `grid` graphics, not `base` graphics. `ggplot` can therefore not be used with `par`. To combine several `ggplot`s, see e.g. [**this Q&A**](http://stackoverflow.com/questions/7993722/creating-arbitrary-panes-in-ggplot2) and several links therein. To combine `base` and `grid` graphics, see package `gridExtra` and search SO. – Henrik Jun 27 '14 at 18:36

1 Answers1

0

You are attempting to combine grid plots (ggplot2) and base plots (boxplot), but those 2 types of plotting do not play nicely together (hence the cryptic warning).

The simplest solution is to only use one of grid or base plots by replacing the call to ggplot with a call to plot or other functions (base only option) or using grid based functions to do the boxplots (lattice package also uses grid) then use functions from the grid package to arrange the multiple plots.

If you really want to combine grid and base plots then you can use the gridBase package, but this is going to require understanding both types of graphics quite well.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110