[R-sig-Geo] My code mistakenly prints multiple PDF pages

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Sun Sep 11 11:14:51 CEST 2016


Jim,

 have you fixed this? There's a lot to look at here and is hard to see
what is going on because most of it is probably irrelevant to your
problem, and we can't run your code because we don't have your data or
shapefiles.

But if you run this with a screen device, does it put all the plots on
one figure? To get everything on one PDF page it should all show on
one screen device.

You seem to have several plot functions with "add=FALSE", which will
start a new plot, which with the PDF device probably will start a new
page.

This is all mostly guesswork because we can't run your code. Try and
whittle it down to the minimal code that shows the problem, and if
possible use some of R's demo data or generate things in any script
you post to the list.

Barry





On Sun, Sep 4, 2016 at 7:24 PM, Jim Burke <javajimburke at gmail.com> wrote:
> The code below should print ONE page but instead prints its different plots
> on THREE pages
> Please take a look and let me know my mistake. I appreciate any and all
> insight.
>
> tmpPDFName = paste("HD_", shp$LD,"_African_American", ".pdf", sep = "",
> collapse = NULL)
> pdf(file = tmpPDFName,
>       width=8.5, height=10, onefile=TRUE, family="Helvetica",
>       paper="a4r", encoding= "default",
>       bg="transparent", fg="black", pointsize=16, pagecentre=TRUE,
> colormodel="srgb",
>       useDingbats=TRUE, useKerning=TRUE, fillOddEven=FALSE, compres=TRUE)
>
> # CENSUS BLOCKS
> HD_census$Black[is.na(HD_census$Black)] <- 0
> HD_census$Total[is.na(HD_census$Total)] <- 0
> add.alpha(brewer.pal(5,'Reds'),0.5)
> my_shades <- auto.shading( ifelse(HD_census$Total==0, 0,
> (HD_census$Black/HD_census$Total)/100 ), n = 5, cutter = rangeCuts, cols =
> brewer.pal(5, "Reds"))
> GISTools::choropleth(HD_census, ifelse(HD_census$Total==0, 0,
> (HD_census$Black/HD_census$Total)/100 ),
>    shading = my_shades, border="transparent")
> title(main = "Africian American Percentages", col = "red", font = 3)   #
> Horizontal HD114
>
> # POLYGON OUTLINE
> #plot.new()
> #par(new=TRUE)
> plot (shp, add=FALSE, lwd = 4, border='green' )
>
> # POLYGON OUTLINE TITLES
> MyTitle <- paste('House District', shp$LD, shp$Candidate, shp$HD_Phone,
> sep="\n")
>
> # POLYGON OUTLINE Insert date and time label. At lower left corner.
> today <- Sys.Date()
> str_today <- format(Sys.time(),"%m/%d/%Y %I:%M %p")
> str_text <- paste("Created:", str_today, sep=" ")
> ##  "usr" returns a vector containing xleft, xright, ybottom, ytop.
> usr <- par( "usr" )
> text( usr[ 1 ], usr[ 3 ], str_text,  adj = c( 0, 0 ), col = 'darkgreen',
> cex=.9 )
>
> # PLOT PRECINCTS INSIDE LARGER POLYGON
> plot (HD_Precincts, add=FALSE, lwd = 2, border='blue' )
>
> myLabel <- paste( bquote(.(HD_Precincts$PCT)),
> bquote(.(HD_Precincts$First_Name)),
> bquote(.        (HD_Precincts$Last_Name)), bquote(.(HD_Precincts$Phone)),
>  sep = '\n', collapse = NULL)
> # PLOT PRECINCT LABELS
> HDShp_LatLong <- polygonsLabel(HD_Precincts,
>    labels = myLabel, method = c("maxdist",
>    "buffer", "centroid", "random", "inpolygon")[2],
>    gridpoints = 60, polypart = c("all","largest")[2],
>    cex = .4, col="blue", doPlot = TRUE)
> # Close and write to PDF
> dev.off()
>
> Thanks,
> Jim
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list