sp bubble plot in ggplot2

352 views
Skip to first unread message

Swagath Navin

unread,
Jul 20, 2011, 6:58:12 AM7/20/11
to ggp...@googlegroups.com
Dear list members,

I am a complete new user of ggplot2. I am trying to replicate a bubble
plot with positive and negative values in ggplot2 which i made using
"sp" package. In ggplot2 i get two legends. I want to combine the
legends in ggplot and make them look like the legend of sp plot. How i
can join the two legends. Here scode i used.

library(ggplot2)
library(sp)

x<-c(198500, 205500, 215500, 214500, 200500, 266500, 266500, 214500,
163600, 170500, 163500, 203500, 263500, 229500, 228500, 243500)
y<-c(586500, 580500, 542500, 549500, 590500, 563500, 564500, 544500,
454500, 451500, 454500, 561500, 496500, 499500, 499500, 511500)
value=c(-0.171316085, 0.108092767, -0.498076668, -0.381020035, 0.6,
-0.571411677, 0.1, 0.3, -0.73060219, 1, 1.2, -1.034438877, -0.755720353,
1.1, -1.222060319, -0.73948598)

dum=data.frame(x=x, y=y, value=value)
coordinates(dum) <- ~x+y
bubble(dum["value"])

ggplot(dum, aes(x, y, size = abs(value), color=value)) + geom_point() +
scale_colour_gradient2('value')

Thanks for your time.


Cheers,
Navin

ggplot.pdf
sp.pdf

Philip Parker

unread,
Jul 20, 2011, 7:20:19 AM7/20/11
to ggp...@googlegroups.com
Hmm my first attempt at sending this did not work. If for whatever
reason the first email does turn up, please accept my apologise.

Anywho I am trying to use facet wrap but it keeps returning an error
(roughly translated from German there is a failure in the dimensions of
the grid). The factet_grid however works fine. While it is probably
something simple I am missing could someone take a look at the code
below and let me know where I am going wrong?

Cheers,

Phil


x <- rnorm (24, mean = 0, sd = .4)
y <- rnorm (24, mean = 0, sd = .4)
group <- as.factor(c (rep('a',6), rep('b',6), rep('c',6), rep('d',6) ) )
my_data <- data.frame (group, x, y)

library(ggplot2)
lm_smooth <- geom_smooth (method = lm, size = 1, fill = 'black', se =
FALSE, colour = "black")

fac_grid<- qplot(x, y, data = my_data) + lm_smooth + facet_grid(~
group) #works
fac_wrap<- qplot(x, y, data = my_data) + lm_smooth + facet_wrap(~
group, nrow = 2) #does not work

Swagath Navin

unread,
Jul 20, 2011, 10:09:44 AM7/20/11
to ggp...@googlegroups.com
ggplot.pdf
sp.pdf

Dennis Murphy

unread,
Jul 20, 2011, 11:39:42 AM7/20/11
to Philip Parker, ggp...@googlegroups.com
Hi:

You've named one of your variables to correspond with the name of an
aesthetic in ggplot2, which is "well-known" on this list to create all
sorts of havoc with ggplots. If you rename group to gp, say, the
problem goes away:

x <- rnorm (24, mean = 0, sd = .4)
y <- rnorm (24, mean = 0, sd = .4)

gp <- as.factor(c (rep('a',6), rep('b',6), rep('c',6), rep('d',6) ) )
my_data <- data.frame (gp, x, y)


lm_smooth <- geom_smooth (method = lm, size = 1, fill = 'black', se =
FALSE, colour = "black")

p <- ggplot(my_data, aes(x, y)) + geom_point() + lm_smooth

# Both of these now work:
p + facet_grid(~ gp)
p + facet_wrap(~ gp, nrow = 2)

##...or equivalently,
q <- qplot(x, y, data = my_data) + lm_smooth
q + facet_grid(~ gp)
q + facet_wrap(~ gp, nrow = 2)

Moral: Don't name your variables group, size, alpha, fill, colour,
color or any other name that would clash with an aesthetic in one or
more of the geoms. Since R is case-sensitive, if you capitalized the
names, that would also take care of the problem. When the ggplot2
renovation project is complete, this should no longer be an issue, but
for now it's still something to keep in mind.

HTH,
Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Dennis Murphy

unread,
Jul 20, 2011, 12:33:41 PM7/20/11
to Swagath Navin, ggp...@googlegroups.com
Hi:

Nothing like starting out with an easy plot :)

ggplot2 has what are called 'fortify' functions for certain types of
spatial objects, but not SpatialPointsDataFrame, so the first step is
to use a regular data frame rather than a spatial one. Secondly, to
get a single legend in ggplot2, the title name and breaks have to
align, which requires a little planning.

I generated two versions of this plot below; the first is more or less
the 'default' in ggplot2, the second is closer to the lattice version
you posted (which got through the first time, BTW).

The 'trick' is to define a factor for the value variable using the
cut() function, which is used to discretize a numeric variable. One
can then use this factor with both the size and color aesthetics and
use scale_*_manual() to manipulate size/color combinations. The second
argument of cut() defines the endpoints of the subintervals (right
closed by default - see ?cut if you want to change this) and the third
argument provides the label names for the subintervals.

# Data manipulation phase:

x<-c(198500, 205500, 215500, 214500, 200500, 266500, 266500, 214500,
163600, 170500, 163500, 203500, 263500, 229500, 228500, 243500)
y<-c(586500, 580500, 542500, 549500, 590500, 563500, 564500, 544500,
454500, 451500, 454500, 561500, 496500, 499500, 499500, 511500)
value=c(-0.171316085, 0.108092767, -0.498076668, -0.381020035, 0.6,
-0.571411677, 0.1, 0.3, -0.73060219, 1, 1.2, -1.034438877,
-0.755720353, 1.1, -1.222060319, -0.73948598)

dum2 <- data.frame(x=x, y=y, value=value)
dum2$val <- cut(dum2$value, c(-Inf, seq(-1.0, 1.0, by = 0.5), Inf),
labels = seq(-1.0, 1.5, by = 0.5))

## Basic ggplot:
ggplot(dum2, aes(x, y, size = val, color=val)) + geom_point() +
scale_colour_manual("value", breaks = levels(dum2$val),
values = rep(c('red', 'green'), each = 3)) +
scale_size_manual("value", breaks = levels(dum2$val),
values = c(6, 4, 2, 2, 4, 6))

The breaks = argument corresponds to the factor levels, the values =
argument to the values of color or size that we want used in their
respective legends. Notice that the title and breaks are the same in
each legend; this is what is required in order to merge them.

To get something similar to the lattice version, we need to turn off
several features: axis labels, tick marks and grid lines. The 'eraser'
is theme_blank(), which is called inside an opts() statement,
discussed in detail in Chapter 8 of the ggplot2 book. If you don't
have (access to) the book, the place to learn about opts() is the
ggplot2 wiki, which is a tightly held secret by members of this list
:)
https://github.com/hadley/ggplot2/wiki/%2Bopts%28%29-List

To change the intervals or names of the intervals, modify the cut()
function that generates dum2$val.

HTH,
Dennis

Hadley Wickham

unread,
Aug 15, 2011, 7:55:08 AM8/15/11
to Dennis Murphy, Philip Parker, ggp...@googlegroups.com
> You've named one of your variables to correspond with the name of an
> aesthetic in ggplot2, which is "well-known" on this list to create all
> sorts of havoc with ggplots. If you rename group to gp, say, the
> problem goes away:

This is fixed (finally!) in the development version of ggplot2.

Hadley


--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages