facet_wrap error

322 views
Skip to first unread message

Ruben Behnke

unread,
Nov 21, 2016, 5:28:38 PM11/21/16
to ggplot2

Hi,

 

   I am trying to create a grid of maps, by month, using the facet_wrap function, as in the following code.  This code was working previously.  I am not able to figure out what's wrong here, but I keep getting the following error.  What am I doing wrong?  The data is attached.  Thank you!!

 

Ruben Behnke

 

Error: length(width) == 1 || length(width) == n is not TRUE
In addition: Warning messages:
1: `axis.ticks.margin` is deprecated. Please set `margin` property  of `axis.text` instead 
2: `panel.margin` is deprecated. Please use `panel.spacing` property instead 


library(raster)
library(ggplot2)
library(openair)
library(maps)
library(gridExtra)

 

canada <- getData("GADM",country="CAN",level=1)
provinces <- c("British Columbia", "Alberta","Saskatchewan","Manitoba","Ontario",canada$NAME_1[13],"New Brunswick")
ca.provinces <- canada[canada$NAME_1 %in% provinces,]
cadf <- fortify(ca.provinces)

 

theme_clean <-  function(base_size=12){
  require(grid)
  theme_grey(base_size) %+replace%
    theme(
      axis.title = element_blank(),
      axis.text = element_blank(),
      panel.background = element_blank(),
      panel.grid = element_blank(),
      axis.ticks.length = unit(0,'cm'),
      axis.ticks.margin = unit(0,'cm'),
      panel.margin = unit(0,'lines'),
      plot.margin = unit(c(0,0,0,0),'lines'),
      complete=TRUE)}

 

Monthly_TdBias <- read.csv('K:\\Paper1\\New_Bias_Data\\TdHourly_ID_Monthly_ALL_MB_SHORT.csv')
Monthly_TdBias$ID <- as.character(Monthly_TdBias$ID); Monthly_TdBias$Month <- as.factor(Monthly_TdBias$Month)

 

png("K:\\Paper1\\Graphics_Diurnal_Paper\\NEW_Hourly_Biases\\Monthly_MB_MAXVPD.png",width=7500,height=7500,res=650)
states_map <- map_data('state')
ggplot(data=Monthly_TdBias) + geom_polygon(data=states_map,aes(x=long,y=lat,group=group),colour='black',fill='white') +
  geom_polygon(data=cadf,aes(x=long,y=lat,group=group),colour='black',fill='white') +
  geom_point(data=Monthly_TdBias, aes(x=Lon, y=Lat, group=NULL, color=Max_Bias),size=0.8, pch=19) + coord_map(xlim = c(-130.8,-66),ylim=c(24,53)) +
  scale_color_gradient2(limits = c(-10,10),low='blue',mid='lightgrey',high='red',midpoint=0) + theme_clean() + facet_wrap(~Month,ncol=3) +
  theme(panel.spacing = unit(c(0.2, 0.2, 0.2, 0.2), "cm")) +
  theme(strip.text.x = element_text(size = 11, colour = "black", face='bold')) +
  theme(legend.key.height = unit(4, "cm")) + ggtitle('Mean Daily Bias in Max VPD when assuming Constant Td (Pa)') +
  theme(plot.title = element_text(size = 11, face = "bold")) +
  theme(legend.title = element_blank()) + theme(legend.text=element_text(size=11,face='bold'))
dev.off()

TdHourly_ID_Monthly_ALL_MB_SHORT.zip

Sam Albers

unread,
Nov 21, 2016, 6:13:29 PM11/21/16
to Ruben Behnke, ggplot2
Hi Ruben,

Without actually testing your data, it seems clear to me that the
problem is not with facet_wrap and rather with some of the deprecated
arguments you are using for your theme. Try commenting out all the
theme stuff and see if your code runs.

HTH,

Sam
> --
> --
> 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
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Behnke, Ruben

unread,
Nov 21, 2016, 6:26:41 PM11/21/16
to Sam Albers, ggplot2
Thanks, Sam. I didn't realize there were changes to the panel.margin theme.

Ruben

________________________________________
From: Sam Albers [tonights...@gmail.com]
Sent: Monday, November 21, 2016 4:12 PM
To: Behnke, Ruben
Cc: ggplot2
Subject: Re: facet_wrap error
Reply all
Reply to author
Forward
0 new messages