stat_density2 area

32 views
Skip to first unread message

Michel Barbosa

unread,
Mar 10, 2009, 6:10:07 PM3/10/09
to ggplot2
Dear list,

I've started out with plotting bubble charts for my dataset, but now
I'm advancing to kernel density estimation surfaces.

The following code produces http://skitch.com/mbarbosa/b8uup/qobee1

library(ggplot2)
library(Cairo)

lng <- c(4.476821,4.485581,4.498923,4.490789,4.497989)
lat <- c(51.915445,51.916742,51.90042,51.887574,51.910305)
rating <- c(7.7,0.0,0.0,0.0,5.0)

info <- data.frame(lng, lat, rating)

no_margins <- opts(
legend.position = 'none',
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")
)

# Setup

myplot <- ggplot(data = info, aes(x = lng, y = lat))

# Layer 1

myplot <- myplot +
stat_density2d(geom="tile", aes(x = lng, y = lat, fill = ..density..),
contour = FALSE) +
scale_fill_gradient("Count", low = "yellow", high = "red") +
coord_cartesian(xlim=c(4.472637176513672, 4.527568817138672), ylim=c
(51.878928367413394, 51.92976563010761))

myplot <- myplot + no_margins

Cairo(file="plot.png", type='png', bg='transparent', width=320,
height=480)
print(myplot)

------------------------------------------

However, if I leave out the

coord_cartesian(xlim=c(4.472637176513672, 4.527568817138672), ylim=c
(51.878928367413394, 51.92976563010761))

then the plot becomes:

http://skitch.com/mbarbosa/b8uwm/map2

What I'd like to do is have the red area to be positioned exactly on
top of the pinpoints. The area to the right of the pinpoints should be
yellow again. Looking at the first plot, the surface seems good.
However, the surrounding area of the map, which is 320 by 480 pixels,
is not coloured at all. Looking at map2, the size of the plot is big
enough, but it should really be positioned the left, on top of the
pinpoints. What am I doing wrong?

Kind regards,

Michel

hadley wickham

unread,
Mar 14, 2009, 12:07:23 PM3/14/09
to Michel Barbosa, ggplot2
> What I'd like to do is have the red area to be positioned exactly on
> top of the pinpoints. The area to the right of the pinpoints should be
> yellow again. Looking at the first plot, the surface seems good.
> However, the surrounding area of the map, which is 320 by 480 pixels,
> is not coloured at all. Looking at map2, the size of the plot is big
> enough, but it should really be positioned the left, on top of the
> pinpoints. What am I doing wrong?

I think you'll need to set both the x and y limits (with xlim and
ylim) to the complete range that you're interested in, since it's
larger than the range of the data. You may find it easier to do the
kernel density estimate yourself so you can get more control. The
essence of stat_density2d is:

dens <- safe.call(MASS::kde2d, c(df, n=100, ...))
df <- with(dens, data.frame(expand.grid(x = x, y = y), z = as.vector(z)))

Hopefully that will get you started.

Hadley

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

Reply all
Reply to author
Forward
0 new messages