Why are point sizes in mm instead of npc?

275 views
Skip to first unread message

Andrew Winterman

unread,
Apr 1, 2012, 7:11:41 PM4/1/12
to ggplot2
Hey guys,

I was wondering if there's a reason ggplot points are set mm instead of npc. This has caused issues for me when displaying graphics on different monitors- the size of points relative to the size of the plotting canvas can vary dramatically, and I could see it leading to errors in graphical display of information as well. Is there an easy way to set the output to npc?

I wrote the following example to demo the issue. The same code, with syntax highlighting is available here


library(ggplot2)

dta <- #make data frame 
data.frame(x = rnorm(1:10), y = rnorm(1:10), size = runif(10) )

ex <- #make plot 
qplot(data= dta,x = x, y =y, size = as.numeric(size), geom = "point", alpha = 0.5) + 
scale_size_continuous(guide = FALSE, range = c(4,20) )

#draw plot on big canvass 
quartz(width = 10, height = 7) 
print(ex+opts(title = "these points could be reasonably sized") )

#draw plot on small canvass 
quartz(width = 4, height = 2.8)
print(ex+ opts(title = "these points are \n less reasonably sized") )

#done with these canvases 
dev.off(); dev.off()

#### I think the output should look like this ###

library(grid) #I'm going to edit the grobs!

#I'm still a newbie with grid, so forgive any inelegencies

pl <- ggplotGrob(ex); dev.off();

### big window ###
quartz(width = 10, height = 7) 
grid.draw(pl)

#setting a few variables for plot editing 

pointsName <- grid.ls()[[1]][grep("geom_point.points", grid.ls()[[1]])] 
npcSizes <- convertUnit(pl$children$`panel-3-4`$children$geom_point.points$size, 'npc')

#apparently this isn't working correctly, point sizes on the big plot should
#remain the same in both unit systems. I'm seeing a slight change.

#convert points to npc.
grid.edit( pointsName , size = npcSizes ) 

### small window ###
quartz(width = 4, height = 2.8)
grid.draw(pl)

pointsName <- grid.ls()[[1]][grep("geom_point.points", grid.ls()[[1]])]
grid.edit( pointsName , size = npcSizes )

#you can toggle through the histories on the two quartz windows to get direct
#comparisons.


#The default behavior seems dangerous to me. What if the data units of the circles
#were the same as the data units of the x and y axis? For example, what if 
#this was a map, with miles on the x and y axis, and the circles represented 
#distance from a point of interest. We'd want the circles to remain the same
#relative to the plotting window, otherwise we're showing different information.
#Currently, if the points are plotted on a smaller window, they represent a
#larger radius.

baptiste auguie

unread,
Apr 1, 2012, 9:28:37 PM4/1/12
to Andrew Winterman, ggplot2
From what I recall from a previous similar discussion on this list,
the point sizes are specified in absolute units to be consistent
across coordinate systems. In particular, think of what would happen
to points of various shapes when the x and y axes have different
scales and/or represent different physical dimensions. Ditto for
coordinate transformations such as coord_polar(), etc.
If you want to add a circle in cartesian coordinates indicating a
distance from a centre, then geom_point() is not the appropriate geom
for this task. You should probably use geom_polygon instead and
construct a circle manually with many vertices. That way, it will
respect the coordinates, even after munching.

I recently updated an experimental geom_ngon() to draw regular
polygons, with 50 vertices you get something akin to a circle,
see https://gist.github.com/2224724

HTH,

baptiste

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

Hadley Wickham

unread,
Apr 1, 2012, 10:35:19 PM4/1/12
to baptiste auguie, Andrew Winterman, ggplot2
> From what I recall from a previous similar discussion on this list,
> the point sizes are specified in absolute units to be consistent
> across coordinate systems. In particular, think of what would happen
> to points of various shapes when the x and y axes have different
> scales and/or represent different physical dimensions. Ditto for
> coordinate transformations such as coord_polar(), etc.
> If you want to add a circle in cartesian coordinates indicating a
> distance from a centre, then geom_point() is not the appropriate geom
> for this task. You should probably use geom_polygon instead and
> construct a circle manually with many vertices. That way, it will
> respect the coordinates, even after munching.

Yes, exactly. It's also not obvious to me how if you have a
non-square plot what governs the shape of a "circle" with dimension
specified in npc.

> I recently updated an experimental geom_ngon() to draw regular
> polygons, with 50 vertices you get something akin to a circle,
> see https://gist.github.com/2224724

I wonder if we should also have a geom_circle that uses circleGrob.
Maybe it could just work for Cartesian coordinate systems.

Hadley


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

Winston Chang

unread,
Apr 1, 2012, 10:50:08 PM4/1/12
to Hadley Wickham, baptiste auguie, Andrew Winterman, ggplot2
Yes, exactly.  It's also not obvious to me how if you have a
non-square plot what governs the shape of a "circle" with dimension
specified in npc.

If it's drawn with circleGrob, the npc size will be relative to the shorter of the two axes. So if the plotting area is 5x10 inches and the npc radius is 0.2, then the radius of the circle will be 1 inch. With some additional code, it's possible to get it to set the radius relative to the x or y axis (this is something that had to be done with geom_dotplot).


> I recently updated an experimental geom_ngon() to draw regular
> polygons, with 50 vertices you get something akin to a circle,
> see https://gist.github.com/2224724

I wonder if we should also have a geom_circle that uses circleGrob.
Maybe it could just work for Cartesian coordinate systems.
 
How would you feel about allowing the user to specify the unit in geom_point? Actually, I can think of a number of possible changes to geom_point that have been discussed here and on the dev list:
- Specify units (with option of making relative to x or y axis)
- Consistent sizes for different sizes (as in a post by Baptiste several weeks ago)
- Consistent use of fill/colour across different shapes


-Winston

Andrew

unread,
Apr 2, 2012, 11:09:49 AM4/2/12
to ggp...@googlegroups.com
Isn't the current specification of point size range arbitrary? Couldn't we just pick reasonable default values, maybe based on the current ones for a reasonably sized plotting windows?

I see why geom_circle is a more appropriate geom in the circumstance I outlined above, but I still think using fixed coordinates for size in geom_point is problematic. I've saved bubble plots to pdf on my machine, and then opened them on a high resolution monitor only to have them nearly illegible because of the relative change in point size. For the record I used the save as feature on the quartz window.

geom_circle is also nicely explicit for a new learner. Thanks!

Andrew

unread,
Apr 2, 2012, 11:11:50 AM4/2/12
to ggp...@googlegroups.com
Isn't the current specification of point size range arbitrary? Couldn't we just pick reasonable default values, maybe based on the current ones for a reasonably sized plotting windows?

I see why geom_circle is a more appropriate geom in the circumstance I outlined above, but I still think using fixed coordinates for size in geom_point is problematic. I've saved bubble plots to pdf on my machine, and then opened them on a high resolution monitor only to have them nearly illegible because of the relative change in point size. For the record I used the save as feature on the quartz window.

geom_circle is also nicely explicit for a new learner. Thanks!

On Sunday, April 1, 2012 7:50:08 PM UTC-7, winston wrote:
On Sunday, April 1, 2012 7:50:08 PM UTC-7, winston wrote:
On Sunday, April 1, 2012 7:50:08 PM UTC-7, winston wrote:
On Sunday, April 1, 2012 7:50:08 PM UTC-7, winston wrote:
On Sunday, April 1, 2012 7:50:08 PM UTC-7, winston wrote:

Hadley Wickham

unread,
Apr 4, 2012, 1:17:42 PM4/4/12
to Andrew, ggp...@googlegroups.com
> Isn't the current specification of point size range arbitrary? Couldn't we
> just pick reasonable default values, maybe based on the current ones for a
> reasonably sized plotting windows?
>
> I see why geom_circle is a more appropriate geom in the circumstance I
> outlined above, but I still think using fixed coordinates for size in
> geom_point is problematic. I've saved bubble plots to pdf on my machine, and
> then opened them on a high resolution monitor only to have them nearly
> illegible because of the relative change in point size. For the record I
> used the save as feature on the quartz window.

But why should geom_point be special? The same reasoning should apply
for geom_line, geom_text, ...

In general, for high-res displays, I think you're better of keeping
the physical size fixed, but increasing the resolution.

Reply all
Reply to author
Forward
0 new messages