Omitting values from legend

58 views
Skip to first unread message

Bigbee, Tony

unread,
Apr 26, 2011, 12:27:15 PM4/26/11
to ggp...@googlegroups.com
I have a variable --pause-- with values 0,1,3,5, that I'm using for point sizes, but the legend shows point symbols for 2,4 that aren't in the graph. How can I omit those?

ggplot(e,aes(id, rate,colour=flow))
p + geom_point(aes(size=pause),legend_param=list(e$pause)) + ylab("rate(%)") + scale_y_continuous(limits = c(55,100)) + opts(axis.title.x = theme_text(colour = "#000000", face = "bold"), axis.title.y = theme_text(face="bold"))

Thanks for any advice.

Tony Bigbee


Brandon Hurr

unread,
Apr 26, 2011, 1:06:05 PM4/26/11
to Bigbee, Tony, ggp...@googlegroups.com
I'll bet that it's treating pause as a continuous variable. 

If you put size=as.factor(pause) does it do what you want? 

B





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

Joshua Wiley

unread,
Apr 26, 2011, 1:07:53 PM4/26/11
to Bigbee, Tony, ggp...@googlegroups.com
Hi Tony,

Depending on the type of variable "pause" is, you can probably do
something like:

+ scale_size_discrete("Legend Name", breaks = c(0, 1, 3, 5), labels =
c(0, 1, 3, 5))

Can you create an example dataset? Or send the output of str(pause)?

Cheers,

Josh

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

--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

Bigbee, Tony

unread,
Apr 26, 2011, 1:12:51 PM4/26/11
to Joshua Wiley, ggp...@googlegroups.com
Josh,

Thank you for your idea.

str(e$pause)
int [1:16] 1 1 3 1 3 3 5 3 5 5 ...

I see this error when I add on your suggestion:
Error: Continuous variable (Legend Name) supplied to discrete scale_size_discrete.

I'll prep a small test data set shortly, unless something comes to mind.

Tony

Brandon Hurr

unread,
Apr 26, 2011, 1:18:41 PM4/26/11
to Bigbee, Tony, Joshua Wiley, ggp...@googlegroups.com
Ah, I forgot size doesn't play nice with discrete variables.

Joshua Wiley

unread,
Apr 26, 2011, 1:19:35 PM4/26/11
to Bigbee, Tony, ggp...@googlegroups.com
Hi Tony,

Looks like Brandon's suggestion might help. If you convert it from
continuous to factor, you will just get the discrete sizes.
Alternately, if you still wanted to specify particular breaks, you
would need to switch from scale_size_discrete to scale_size_continuous
since it is integer class, not discrete right now.

HTH,

Josh

Joshua Wiley

unread,
Apr 26, 2011, 1:22:38 PM4/26/11
to Brandon Hurr, ggp...@googlegroups.com
On Tue, Apr 26, 2011 at 10:18 AM, Brandon Hurr <brando...@gmail.com> wrote:
> Ah, I forgot size doesn't play nice with discrete variables.

Eh?

I thought factor() was a great suggestion, what's not nice about this?

dev.new()
ggplot(mtcars, aes(hp, mpg, size = carb)) + geom_point()
dev.new()
ggplot(mtcars, aes(hp, mpg, size = factor(carb))) + geom_point()

Bigbee, Tony

unread,
Apr 26, 2011, 1:22:39 PM4/26/11
to Joshua Wiley, ggp...@googlegroups.com
Josh,

Scale_size_continuous works perfectly. Thank you and Brandon for your help.

Brandon Hurr

unread,
Apr 26, 2011, 1:25:31 PM4/26/11
to Joshua Wiley, ggp...@googlegroups.com
I thought he was using my addition and not yours when he had the error...
Error: Continuous variable (Legend Name) supplied to discrete scale_size_discrete.

Good that my intuition didn't fail me.

B

Hadley Wickham

unread,
Apr 26, 2011, 1:29:03 PM4/26/11
to Joshua Wiley, Brandon Hurr, ggp...@googlegroups.com
On Tue, Apr 26, 2011 at 12:22 PM, Joshua Wiley <jwiley...@gmail.com> wrote:
> On Tue, Apr 26, 2011 at 10:18 AM, Brandon Hurr <brando...@gmail.com> wrote:
>> Ah, I forgot size doesn't play nice with discrete variables.
>
> Eh?
>
> I thought factor() was a great suggestion, what's not nice about this?
>
> dev.new()
> ggplot(mtcars, aes(hp, mpg, size = carb)) + geom_point()
> dev.new()
> ggplot(mtcars, aes(hp, mpg, size = factor(carb))) + geom_point()

Well, size is no longer proportional to carb...

Hadley

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

Brandon Hurr

unread,
Apr 26, 2011, 2:02:57 PM4/26/11
to Hadley Wickham, Joshua Wiley, ggp...@googlegroups.com
Can you force it to be so?

-B

Joshua Wiley

unread,
Apr 26, 2011, 2:59:45 PM4/26/11
to Brandon Hurr, ggp...@googlegroups.com
Well, if you want proportionality, you are basically talking about a
continuous variable at which point, you can just customize particular
break points and labels (as with scale_size_continuous()) rather than
factor()ing. factor() is just a convenient way to numerify (submitted
to the OED, but rejected ;) categorical data (or in Tony's case,
"drop" from the legend unused numerical entries by converting 0, 1, 3,
5 to 1, 2, 3, 4 esentially (the underlying numeric frame of the
factor).

Josh

--

Reply all
Reply to author
Forward
0 new messages