geom_point size and colour and different axis limits with facet

407 views
Skip to first unread message

Manuel Spínola

unread,
Oct 18, 2010, 6:46:38 PM10/18/10
to ggplot2
Dear list members,

2 questions:

I want to do a type of balloon plot with geom_point to show different size regarding a continuous variable for farms located at different locations (I am using geographic coordinates).

1. Is it possible to control for size and color  by a continuous variable in the geom_point?

p = ggplot(geo1, aes(x, y))
p + geom_point(aes(size = md)) + scale_size(name = "Indice", breaks = c(0.1, 0.5, 1, 5, 10, 50))

2. Is it possible to set 2 different limits in the x axis working with facet?

Thank you very much in advance.

Manuel

--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS

Ista Zahn

unread,
Oct 18, 2010, 7:01:25 PM10/18/10
to Manuel Spínola, ggplot2
Hi Manuel,

Can you clarify your question (I'm not sure what you mean by "control
for size and color") and provide a reproducible example?

-Ista

> --
> 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
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Manuel Spínola

unread,
Oct 18, 2010, 7:16:05 PM10/18/10
to Ista Zahn, ggplot2
Thank you very much Ista.

Yes, I am attaching the data (indicegeo).


p = ggplot(geo1, aes(x, y))
p + geom_point(aes(size = md)) + scale_size(name = "Indice", breaks = c(0.1, 0.5, 1, 5, 10, 15, 20, 25, 50)) + theme_bw()

In the code I am setting the point size according to the variable md, but I also want the points to be in different colors (colour = md).

Regarding to the other question, the data set has 2 different groups (geographically different) and I would like to do 2 facets to show each group but with different limits for the x axis.  I am attaching a plot.

Best,

Manuel
facet.png
indicegeo.txt

Ista Zahn

unread,
Oct 18, 2010, 10:10:19 PM10/18/10
to Manuel Spínola, ggplot2
Hi Manuel

On Mon, Oct 18, 2010 at 7:16 PM, Manuel Spínola <mspin...@gmail.com> wrote:
> Thank you very much Ista.
>
> Yes, I am attaching the data (indicegeo).

Great, thanks for the example.

>
> p = ggplot(geo1, aes(x, y))
> p + geom_point(aes(size = md)) + scale_size(name = "Indice", breaks = c(0.1,
> 0.5, 1, 5, 10, 15, 20, 25, 50)) + theme_bw()
>
> In the code I am setting the point size according to the variable md, but I
> also want the points to be in different colors (colour = md).

you just need to tell ggplot what you're telling me (literally!).

p + geom_point(aes(size = md, colour=md)) + scale_size(name =


"Indice", breaks = c(0.1, 0.5, 1, 5, 10, 15, 20, 25, 50)) + theme_bw()

>


> Regarding to the other question, the data set has 2 different groups
> (geographically different) and I would like to do 2 facets to show each
> group but with different limits for the x axis.  I am attaching a plot.

So you need a faceting variable.

p + geom_point(aes(size = md, color=md)) + scale_size(name = "Indice",
breaks = c(0.1, 0.5, 1, 5, 10, 15, 20, 25, 50)) + theme_bw() +
facet_grid(.~sitio, scales="free")

These are pretty standard features, have you browsed http://had.co.nz/ggplot2/?

-Ista

--

Manuel Spínola

unread,
Oct 18, 2010, 10:57:07 PM10/18/10
to Ista Zahn, ggplot2
Thank you very much Ista.

I made a mistake I used colour instead of color.  However, I still have an error message:


> p = ggplot(geo1, aes(x, y))
> p + geom_point(aes(size = md, color = md)) + scale_size(name = "Indice", breaks = c(0.1, 0.5, 1, 5, 10, 15, 20, 25, 50)) + theme_bw()
Error en data.frame(size = c(1.00288566953386, 1.04028941815279, 1.08704410392645,  :
  arguments imply differing number of rows: 9, 1, 56

And regarding to the second question I didn't know about the scale free, but I still have an error too:


> p + geom_point(aes(size = md, color=md)) + scale_size(name = "Indice",
+ breaks = c(0.1, 0.5, 1, 5, 10, 15, 20, 25, 50)) + theme_bw() +

+ facet_grid(.~sitio, scales="free")
Error en data.frame(size = c(1.00288566953386, 1.04028941815279, 1.08704410392645,  :
  arguments imply differing number of rows: 9, 1, 56

I think is when I combine scale_size and scales=free

Best,

Manuel

Manuel Spínola

unread,
Oct 18, 2010, 11:14:08 PM10/18/10
to Ista Zahn, ggplot2
Hi Ista,

I found that point_range color and scale_size don't work together.
Is that possible?

Best,

Manuel

On 18/10/2010 08:10 p.m., Ista Zahn wrote:
p + geom_point(aes(size = md, color=md)) + scale_size(name = "Indice",
breaks = c(0.1, 0.5, 1, 5, 10, 15, 20, 25, 50)) + theme_bw() +
facet_grid(.~sitio, scales="free")



Dennis Murphy

unread,
Oct 18, 2010, 11:35:58 PM10/18/10
to Manuel Spínola, ggplot2
Hi Manuel:

Are you trying to coordinate the colors and sizes in the same legend? If so, then you need to give both scales the same name and the same set of breaks (Hadley mentioned this the other day in another post.)  Does this work?

p <- ggplot(geo1, aes(x = x, y = y, size = md, colour = md))
p + geom_point() + facet_wrap(~ sitio, scales = 'free') +
#    theme_bw() +

    scale_size(name = 'Indice',
                         breaks = c(0.1, 0.5, 1, 5, 10, 15, 20, 25, 50)) +
    scale_colour_gradientn(name = 'Indice', colours = topo.colors(9),

                         breaks = c(0.1, 0.5, 1, 5, 10, 15, 20, 25, 50)) +
    opts(axis.text.x = theme_text(size = 8, vjust = 1)) +
    opts(axis.text.y = theme_text(size = 8, hjust = 1))

Uncomment the line with theme_bw() if you want a white background instead.

HTH,
Dennis

--

Manuel Spínola

unread,
Oct 19, 2010, 12:37:42 AM10/19/10
to Dennis Murphy, Manuel Spínola, ggplot2
Thank you very much Dennis.

That is perfect.

Best,

Manuel
Reply all
Reply to author
Forward
0 new messages