Units with Two Dimensions (Both npc and mm)

40 views
Skip to first unread message

Tim Grilley

unread,
Oct 13, 2014, 1:22:33 PM10/13/14
to ggp...@googlegroups.com
Hello, sometimes it is useful to have units with two dimensions, say, a plot margin that is 1/3rd npc, and also, 3 mm (To help align with grid.arrange and aligning in a matrix). The grid unit function can accommodate this:

> require(grid)
> u=unit(.01,'npc')+unit(3,'mm')
> u
[1] 0.01npc+3mm

However, the ggplot library does not appear to know how to navigate this. Besides fixing a plotwidth/height, and then on modification of those, re-calculating the units, is there any way to do something equivalent? This would help in situations where you know the plot starts ends at 1/3rd npc (From grid align) and 3 mm (From normal plot margin). Thanks for any suggestions/solutions!

Minimal (frivolous) example of desired functionality:
> require(ggplot2)
> require(grid)
> ggplot(diamonds,aes(x=color,y=cut))+theme(axis.ticks.length=unit(.01,'npc')+unit(3,'mm'))
Error in `[.unit.arithmetic`(y, idx) : 
  index out of bounds (unit arithmetic subsetting)

Not so frivolous example (Notice in the two plots the axis tics do not quite overlap between the top histogram and the bottom 2d density plot); if things were perfect, you would set the left side as a mm offset to compensate for the left margin of the 2d density plot, and the right margin as 1/3rd the plot width (npc) + 3mm, with the top and bottom being some constant mm offset:

require(ggplot2)
require(grid)
require(gridExtra)
bins=ggplot(diamonds,aes(x=color,y=cut))+
  stat_bin2d()+
  theme(legend.justification=c(0,1),legend.position=c(0,1),legend.key.width=unit(10,'mm'))+
  scale_fill_gradient2(guide=guide_colorbar(direction='horizontal',title=element_blank()))
themeBlank=theme(legend.position='none'
                 ,axis.title.x=element_blank()
                 ,axis.title.y=element_blank()
                 ,axis.text.x=element_blank()
                 ,axis.text.y=element_blank()
                 ,plot.margin=unit(c(3,-5.5,4,3),'mm'))
empty=ggplot()+geom_point(aes(1,1), colour="white")+
         theme(legend.position='center',axis.ticks=element_blank(), 
              panel.background=element_blank(), 
              axis.text.x=element_blank(), axis.text.y=element_blank(),           
              axis.title.x=element_blank(), axis.title.y=element_blank())
histX=ggplot(diamonds,aes(x=color,y=..count../sum(..count..)*100,fill=cut))+
  geom_bar()+
  scale_fill_brewer('blues')+
  themeBlank+
  theme(axis.text.y=element_text()
        ,axis.title.y=element_text()
        ,plot.margin=unit(c(0,.33,0,0),'npc')#+unit(c(3,3,3,3),'mm')
        ,legend.position=c(1+1/6,.5)
        ,legend.title=element_blank())+
  ylab('Percent')+
  guides(fill=guide_legend(ncol=2))
histY=ggplot(diamonds,aes(x=cut,y=..count../sum(..count..)*100,fill=color))+
  geom_bar()+
  scale_fill_brewer('blues')+
  coord_flip()+
  themeBlank+
  theme(axis.text.x=element_text(),axis.title.x=element_text(),plot.margin=unit(c(7,3,3.5,-5),'mm'),legend.position='right',legend.title=element_blank())+
  ylab('Percent')
grid.arrange(histX,arrangeGrob(bins,histY,ncol=2,widths=c(2,1)),heights=c(1,2))

Very sorry if this is a duplicate post! Thanks for any response,

Tim
Reply all
Reply to author
Forward
0 new messages