Bubble chart size and colour

619 views
Skip to first unread message

Michel Barbosa

unread,
Mar 8, 2009, 2:02:00 PM3/8/09
to ggplot2
Dear list,

I've succesfully plotted a bubble chart for my restaurant data set. My
"info" dataframe consists of longitude / latitude / rating

My ratings continuous values, but for now I've rounded them off to be
discrete values. My problem is the following. I'd like to use 10 fixed
colours for depicting the ratings. The ratings are between 0 and 10.
Moreover, I'd like the points to scale proportionally to their rating.
This allows me to compare the ratings of the restaurants by looking at
the legend for both the point size and colour. I've struggled with the
aesthetics, but couldn't get it right. My code is as follows. How can
I make sure that let's say, the 10 colours are rainbow(10) AND have
the size depend on the rating?

Kind regards,

Michel

library(ggplot2)
library(Cairo)

lng <- c
(4.482566,4.473565,4.483039,4.474304,4.485887,4.474615,4.497905,4.476821,4.476111,4.481135,4.491674,4.482028,4.483648,4.504239,4.51775,4.517735,4.510746,4.517979,4.477751,4.478252,4.47903,4.479929,4.471585,4.47866,4.478029,4.523455,4.490437,4.486032,4.488442)

lat <- c
(51.913126,51.915905,51.91792,51.916159,51.915666,51.91442,51.921744,51.915445,51.916552,51.916185,51.918877,51.918925,51.91889,51.921842,51.916974,51.916979,51.920805,51.918007,51.909958,51.909476,51.908888,51.909112,51.903885,51.909551,51.906958,51.895308,51.907031,51.901382,51.88934)

rating <- c
(8.0,8.0,8.0,8.0,0.0,9.0,8.0,8.0,9.0,7.0,7.0,7.0,7.0,8.0,9.0,9.0,0.0,8.0,8.0,8.0,7.0,7.0,9.0,8.0,7.0,0.0,6.0,7.0,7.0)

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

no_margins <- opts(
legend.background = theme_rect(fill = "white", col= "white", size =
4),
legend.position = c(0.9, 0.5),
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 <- ggplot(data = info, aes(lng, lat)) +
geom_point(aes(size=rating), colour=alpha(rainbow(29), 0.7)) +
scale_size(name='Ratings', limit=c(0, 10), breaks=1:10, to = c(0, 15))
+
coord_cartesian(xlim=c(4.472637176513672, 4.527568817138672), ylim=c
(51.878928367413394, 51.92976563010761)) +
no_margins

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

Michel Barbosa

unread,
Mar 8, 2009, 4:57:48 PM3/8/09
to ggplot2
What I'd like to do is have a rating of 1 map to #FF0000FF, rating of
2 map to #FF9900FF, ... rating of 10 map to #FF0099FF. I'd like to
avoid using different colours each time to represent a specific
rating, My problem is that I can't seem to get the legend with 10
colours and have the points coloured correctly using this legend.

hadley wickham

unread,
Mar 9, 2009, 9:22:21 AM3/9/09
to Michel Barbosa, ggplot2
Hi Michel,

You need to do the same thing you did with size: map colour to
rating, and then find an appropriate scale that performs the mapping
that you want. In this case, I'd say you want scale_gradientn which
allows to you create a mapping to any continuous colour gradient:

ggplot(data = info, aes(lng, lat)) +
geom_point(aes(size=rating, colour = rating)) +
scale_size(name='Ratings', limits=c(0, 10), breaks=1:10, to = c(0, 15)) +
scale_colour_gradientn(limits = c(0, 10), breaks=1:10, colours = rainbow(7))

However, there seems to be a bug with the legend, which I'm looking into now.

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

hadley wickham

unread,
Mar 9, 2009, 9:34:13 AM3/9/09
to Michel Barbosa, ggplot2
On Mon, Mar 9, 2009 at 7:22 AM, hadley wickham <h.wi...@gmail.com> wrote:
> Hi Michel,
>
> You need to do the same thing you did with size:  map colour to
> rating, and then find an appropriate scale that performs the mapping
> that you want.  In this case, I'd say you want scale_gradientn which
> allows to you create a mapping to any continuous colour gradient:
>
> ggplot(data = info, aes(lng, lat)) +
> geom_point(aes(size=rating, colour = rating)) +
> scale_size(name='Ratings', limits=c(0, 10), breaks=1:10, to = c(0, 15)) +
> scale_colour_gradientn(limits = c(0, 10), breaks=1:10, colours = rainbow(7))
>
> However, there seems to be a bug with the legend, which I'm looking into now.

Ooops, the code should be:

ggplot(data = info, aes(lng, lat)) +
geom_point(aes(size=rating, colour = rating)) +

scale_area('Ratings', limits=c(0, 10), breaks=1:10, to = c(0, 15)) +
scale_colour_gradientn('Ratings', limits = c(0, 10), breaks=1:10,
colours = rainbow(7))

And you might want to use scale_area instead of scale_size, to make
the area of the points proportional to rating, rather than there
radius.

Hadley


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

Michel Barbosa

unread,
Mar 9, 2009, 8:23:47 PM3/9/09
to ggplot2
Dear Hadley,

thanks, that did it! My output is now as follows
http://skitch.com/mbarbosa/b8qhg/proportional-symbols

Kind regards,

Michel

On 9 mrt, 14:34, hadley wickham <h.wick...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages