Using ggplot2 with spatstat

834 views
Skip to first unread message

Michel Barbosa

unread,
Mar 1, 2009, 11:07:21 AM3/1/09
to ggplot2
Dear list,

I've just started reading the ggplot2 book and will process my way
through the book as of today. For my research, I'm investigating which
Spatial Data Analysis (SDA) techniques I could use to overlay on my
Google Maps. So far, I've been able to generate a Kernel Density
Estimation surface as a PNG file. However, my Google Maps bounding box
does not match the window of my plot. My questions to you guys is, is
it possible to use ggplot2 in combination with spatstat to produce
plots for my point patterns? Could you provide me with any tips or
ideas?
Your help is much appreciated.

Kind regards,

Michel

Michel Barbosa

unread,
Mar 4, 2009, 9:56:04 AM3/4/09
to ggplot2
My problem is the following. I have a set of points with longitude/
latitude coordinates. I wish to plot these in a bounding box, which
happens to be the same bounding box that my google map uses. My wish
is to have ggplot2 generate and save the plot and have this plot
overlayed on my google maps, using the exact same boundaries/
coordinates. My approach is as follows:

library(Cairo)
library(spatstat)
info <- read.csv(file = "import.csv", sep = ";", strip.white =
TRUE)
window = as.owin(c(4.457187652587891, 4.543018341064453,
51.888464736976516, 51.92023802576216))
pattern <- ppp(info$lng, info$lat, window = window)
Cairo(file="plot.png", type='png', bg='transparent', width=500,
height=300)
par(mar=c(0,0,0,0))
plot(info$lng, info$lat, cex = 0.5, main = '', xlab = '', ylab='',
frame.plot = T, axes = T)
dev.off()


This creates a plot.png as in: http://skitch.com/mbarbosa/bg5rm/plot

My intent is to do this: http://skitch.com/mbarbosa/bgqq8/kde-transparent

However, with spatstat, I can't seem to be able to change the margins
of my plot.png, which results in an overlay that is not correct. My
google map is 500 by 300 pixels. While plot.png has a height of 500
and a width of 300 pixels, I need to get rid of the margins.

That's why I started to look for alternatives. I've started digging
into ggplot2 and it looks very promising.

From your reply I understand that I have to work with dataframes. So
I've started out with doing:

mypoints <- as.data.frame(pattern)
qplot(x, y, data = mypoints)


This results in http://skitch.com/mbarbosa/bg5rh/ggplot

What I need to now is get rid of the grid, have the points plotted in
a bounding box of c(4.457187652587891, 4.543018341064453) and c
(51.888464736976516, 51.92023802576216), with no margins and of size
500 x 300. Could you provide me with tips on how to achieve this?
Your help is dearly appreciated.

Kind regards,

Michel

hadley wickham

unread,
Mar 4, 2009, 11:12:04 AM3/4/09
to Michel Barbosa, ggplot2

+ opts(panel.grid.major = theme_blank(), panel.grid.minor =
theme_blank(), panel.background = theme_blank())

> have the points plotted in
> a bounding box of c(4.457187652587891, 4.543018341064453) and c
> (51.888464736976516, 51.92023802576216),

+ xlim(4.457187652587891, 4.543018341064453)
+ ylim(51.888464736976516, 51.92023802576216)

> with no margins

no_margins <- opts(
axis.line = theme_blank(),
axis.text.x = theme_blank(),
axis.text.y = theme_blank(),
axis.ticks = theme_blank(),
axis.title.x = theme_blank(),
axis.title.y = theme_blank(),
axis.ticks.length = unit(0, "cm"),
axis.ticks.margin = unit(0, "cm"),

panel.background = theme_blank(),
panel.border = theme_blank(),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),

plot.background = theme_blank(),
plot.title = theme_blank(),
plot.margin = unit(c(0, 0, 0, 0), "lines")
)

+ no_margins

> and of size
> 500 x 300. Could  you provide me with tips on how to achieve this?

png("myfile.png", width = 500, height = 300)
print(myplot)
dev.off()

Regards,

Hadley

--
http://had.co.nz/

Michel Barbosa

unread,
Mar 4, 2009, 11:31:05 AM3/4/09
to ggplot2
Dear Hadley,

thanks for your quick reply. I've tried to do the following, but with
no luck:

library(ggplot2)
library(Cairo)
library(spatstat)
info <- read.csv(file = "import.csv", sep = ";", strip.white = TRUE)
window = as.owin(c(4.457187652587891, 4.543018341064453,
51.888464736976516, 51.92023802576216))
pattern <- ppp(info$lng, info$lat, window = window)
ai <- as.data.frame(pattern)

no_margins <- opts(
axis.line = theme_blank(),
axis.text.x = theme_blank(),
axis.text.y = theme_blank(),
axis.ticks = theme_blank(),
axis.title.x = theme_blank(),
axis.title.y = theme_blank(),
axis.ticks.length = unit(0, "cm"),
axis.ticks.margin = unit(0, "cm"),

panel.background = theme_blank(),
panel.border = theme_blank(),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),

plot.background = theme_blank(),
plot.title = theme_blank(),
plot.margin = unit(c(0, 0, 0, 0), "lines")
)

myplot <- qplot(x, y, data = ai, xlim(4.457187652587891,
4.543018341064453), ylim(51.888464736976516, 51.92023802576216)) +
opts

Cairo(file="plot.png", type='png', bg='white', width=500, height=300)
print(myplot)
dev.off()

It says: Error: could not find function "theme_blank"

Did I forget to include / require some ilbrary?

Kind regards,

Michel

On 4 mrt, 17:12, hadley wickham <h.wick...@gmail.com> wrote:
> > This results inhttp://skitch.com/mbarbosa/bg5rh/ggplot

jiho

unread,
Mar 4, 2009, 1:25:44 PM3/4/09
to Michel Barbosa, ggplot2
On 2009-March-04 , at 17:31 , Michel Barbosa wrote:

> myplot <- qplot(x, y, data = ai, xlim(4.457187652587891,
> 4.543018341064453), ylim(51.888464736976516, 51.92023802576216)) +
> opts

it is either:
qplot(blah blah, xlim=c(...), ylim=c(...))
or
qplot(blah blah) + xlim(...) + ylim(...)
xlim and ylim in the second case are just shorthands for
scale_x_continuous(limits=c(...)) and scale_y_cont...

So overall it should be something like:

myplot <- qplot(x, y, data = ai) +
xlim(4.457187652587891, 4.543018341064453) +
ylim(51.888464736976516, 51.92023802576216)) +


opts(panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),

panel.background = theme_blank()) +
no_margins

JiHO
---
http://jo.irisson.free.fr/

Michel Barbosa

unread,
Mar 8, 2009, 1:54:56 PM3/8/09
to ggplot2
Thanks guys. Got it working :)
Reply all
Reply to author
Forward
0 new messages