change size of points

1,918 views
Skip to first unread message

Ross

unread,
Feb 25, 2013, 10:40:06 AM2/25/13
to ggp...@googlegroups.com
I have a dataframe that looks something like this:

DF <- data.frame(index=1:10, points=seq(10, 100, 10))

I've plotted the dataframe, using 'value' to control the size of each point:

ggplot(DF, aes(index, points, size=points)) + geom_point()

In addition to controlling the size of each point in relation to each other, how can I change the absolute size of each point? Or in other words, how can i make each point bigger? I thought this would work, but it has only changed the scale: 

ggplot(DF, aes(index, value, size=5*value)) + geom_point()

William Beasley

unread,
Feb 26, 2013, 11:15:44 AM2/26/13
to ggp...@googlegroups.com
I think the main thing is to set the scale to `identity`.  Below is one of the ways to do it.  I added a little extra code to make the manipulation more explicit.

DF <- data.frame(index=1:10, points=seq(10, 100, 10))
ggplot(DF, aes(index, points, size=points)) + geom_point()

b0 <- 1
b1 <- .5
DF$size <- b0 + b1*DF$points
ggplot(DF, aes(x=index, y=points, size=size)) + geom_point() + scale_size_identity()

Ista Zahn

unread,
Feb 26, 2013, 11:25:24 AM2/26/13
to William Beasley, ggp...@googlegroups.com
On Tue, Feb 26, 2013 at 11:15 AM, William Beasley <wibe...@hotmail.com> wrote:
> I think the main thing is to set the scale to `identity`.

The more typical way would be to set the range argument to the size scale:

ggplot(DF, aes(index, points, size=points)) +
geom_point() +
scale_size_continuous(range=c(4, 10))

Best,
Ista

Below is one of
> the ways to do it. I added a little extra code to make the manipulation
> more explicit.
>
> DF <- data.frame(index=1:10, points=seq(10, 100, 10))
> ggplot(DF, aes(index, points, size=points)) + geom_point()
>
> b0 <- 1
> b1 <- .5
> DF$size <- b0 + b1*DF$points
> ggplot(DF, aes(x=index, y=points, size=size)) + geom_point() +
> scale_size_identity()
>
> http://docs.ggplot2.org/current/scale_size.html
>
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

William Beasley

unread,
Feb 26, 2013, 11:43:41 AM2/26/13
to ggp...@googlegroups.com, William Beasley
I like your way better.  Until I compared the versions, I didn't realize mine dropped the legend for size.

Dennis Murphy

unread,
Feb 26, 2013, 4:32:34 PM2/26/13
to William Beasley, ggp...@googlegroups.com
By default, scale_*_identity() does not generate a legend guide. See
the examples in http://docs.ggplot2.org/current/scale_identity.html
for further details.

Dennis

On Tue, Feb 26, 2013 at 8:43 AM, William Beasley <wibe...@hotmail.com> wrote:
> I like your way better. Until I compared the versions, I didn't realize
> mine dropped the legend for size.
>

Winston Chang

unread,
Feb 26, 2013, 4:44:02 PM2/26/13
to Ista Zahn, William Beasley, ggp...@googlegroups.com
I would suggest using scale_size_area() instead of scale_size_continuous(), which is the default for ggplot2.

scale_size_continuous will make a linear mapping of your data values to the radius of the points, which could be misleading. Your data in this example goes from 10 to 100, and the default radius range for scale_size_continuous is 1 to 6mm (this is set with range=c(1, 6)). So 10 will be mapped to 1mm, and 100 will be mapped to 6mm. When you change your data values, they still get mapped to the range 1-6mm.

You can change the max and min sizes with something like range=c(4,10) -- but even if you do this, the area won't be proportional to the data values, since the mapping is linear with respect to radius, not area.

If, instead, you use scale_size_area, the area will be proportional to the data value. It lets you specify the max size (radius in mm), but not a min size, since that is automatically determined when you have the data and know the max size.

DF <- data.frame(index=1:10, points=seq(10, 100, 10))
ggplot(DF, aes(index, points, size=points)) +
  geom_point() +
  scale_size_area(max_size=10)

-Winston
Reply all
Reply to author
Forward
0 new messages