pattern fill

1,011 views
Skip to first unread message

dM/

unread,
Sep 13, 2010, 6:04:45 AM9/13/10
to ggplot2
Hi

I'm new to the group, so sorry if this has been asked before. Is it
possible to produce a histogram where the fill is not just a solid
colour, but has a pattern fill? (pattern fill - e.g. red diagonal
stripes on a white background)

I'm asking, because in the chart I'm working on, (using
scale_fill_manual) it can be hard to pick distinctive colours.

Also, if it is possible to pattern fill, is there a way to combine it
efficiently with solid colours? e.g. I'd like to display four bars in
a chart, where

- bar 1 = "expected financial loss" - solid colour = red
- bar 2 = "actual financial loss" pattern fill, colour = red diag
strips on solid white background
- bar 3 = "expected borrowing costs" - solid colour = blue
- bar 4 = "expected borrowing costs" pattern fill, colour = blue diag
strips on solid white background

Many thanks


Brandon Hurr

unread,
Sep 13, 2010, 8:18:38 AM9/13/10
to dM/, ggplot2
Last thing I could find about it says no. 


Unfortunately grid (the underlying graphics library that ggplot2 uses)
does not currently support patterns.

Hadley
That was November 2009 though...

Brandon



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

Brandon Hurr

unread,
Sep 13, 2010, 8:21:20 AM9/13/10
to dM/, ggplot2
I would also like to add that I would like to see this happen as well. Much easier to differentiate and match different patterns on a B&W barplot than different shades of grey. 

-B

baptiste Auguié

unread,
Sep 13, 2010, 8:25:27 AM9/13/10
to Brandon Hurr, dM/, ggplot2
Hi,

I think it's still the official state of affairs.

There's an experimental implementation of filling patterns for Grid graphics in the gridExtra package though, with some motivation one could write a ggplot2 geom for it.

library(gridExtra)
example(patternGrob)

HTH,

baptiste

Brandon Hurr

unread,
Sep 13, 2010, 8:49:46 AM9/13/10
to baptiste Auguié, dM/, ggplot2
I've never actually looked at the guts of a geom_* before... how much motivation are we talking about?

B

baptiste Auguié

unread,
Sep 13, 2010, 9:39:25 AM9/13/10
to Brandon Hurr, dM/, ggplot2
On Sep 13, 2010, at 2:49 PM, Brandon Hurr wrote:

I've never actually looked at the guts of a geom_* before... how much motivation are we talking about?

Making a rough new geom is quite doable, the major steps are summarized here:


However, making a *good* geom is another story :)

In the case of pattern fills one would probably have to define new scales as well to map the pattern density and orientation.

baptiste

David Menezes

unread,
Sep 13, 2010, 9:26:51 AM9/13/10
to Brandon Hurr, baptiste Auguié, ggplot2
thanks for the quick response guys.  hopefully this is something that will be possible in future.  for now, i've used the alpha function to basically compare "red" with 50% transparent "red", etc. 
 
not as clear as a pattern chart, but reasonable.
 
thanks again, dM/

Brandon Hurr

unread,
Sep 13, 2010, 10:12:29 AM9/13/10
to baptiste Auguié, dM/, ggplot2
Do you actually need a geom_*? Seems like what you really need is a scale_. Like a hacked version of scale_fill_color() that outputs patterns instead. Or are scales actually geom()s in hiding?

Brandon

baptiste Auguié

unread,
Sep 13, 2010, 12:02:30 PM9/13/10
to Brandon Hurr, ggplot2
you'll need a geom, I think, otherwise the output of your scale won't be turned into a graphical object; there is currently no geom that knows how to draw a patternGrob.

baptiste

Luciano Selzer

unread,
Jan 18, 2011, 2:56:37 PM1/18/11
to Brandon Hurr, ggplot2
Hello, just to know. Has there been any progress in writing a new geom that can use patterns?

Thank you
Luciano


2010/9/13 baptiste Auguié <bapt...@googlemail.com>

Brandon Hurr

unread,
Jan 18, 2011, 3:17:39 PM1/18/11
to Luciano Selzer, ggplot2
I looked into it, but my knowledge of the ggplot subsystem quickly prevented me from understanding what I was doing and I stopped to work on other things. Sadly, that was as far as I got. I would have/still would gladly contribute to something if I can help. I'm mostly good at breaking things though. 

Brandon

baptiste auguie

unread,
Jan 19, 2011, 8:49:12 AM1/19/11
to Brandon Hurr, ggplot2
I haven't found the motivation nor the time to give it a shot either.
With the new ggplot2(3?) infrastructure, perhaps this would make a
good exercise to see how it all works now (I don't know if it's
functional yet, though).

baptiste

Hadley Wickham

unread,
Jan 19, 2011, 9:38:32 AM1/19/11
to baptiste auguie, Brandon Hurr, ggplot2
If anyone wanted to contribute this, the place to start would be
writing a grid grob that supported it. Then I could easily write the
supporting infrastructure in ggplot2.

Hadley

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

Brandon Hurr

unread,
Jan 19, 2011, 12:45:22 PM1/19/11
to Hadley Wickham, baptiste auguie, ggplot2
How close is patternGrob() to being a grid grob? 

Does it need more patterns? 

Brandon

baptiste auguie

unread,
Jan 20, 2011, 3:18:42 AM1/20/11
to Brandon Hurr, ggplot2
patternGrob is a grob already. What's really missing is some work to
improve its implementation, and on the ggplot2 side a simple wrapper
together with appropriate scales. The main question I have is, is it
worth the trouble considering that it would be limited to filling
rectangles?

baptiste

Brandon Hurr

unread,
Jan 20, 2011, 3:38:16 AM1/20/11
to baptiste auguie, ggplot2
I could see it being useful for those dreaded dynamite plots, but also geom_tile. If it could be made to work with geom_polygon & geom_area you could use it to pattern over an area of a map. I see its' main purpose as making graph objects distinctive in the absence of color or over the top of color. I would suggest that is perhaps it's sole purpose (primarily for publication reasons), although I'm sure others could come up with other reasons it could be useful to them.

baptiste auguie

unread,
Jan 24, 2011, 6:31:51 AM1/24/11
to ggplot2
Flicking through the pgf manual, there is now a rather nice collection
of built-in patterns. I believe this is the way to go; patterns should
be implemented at a low level (e.g. by the graphics device), because
they require costly/painful calculations of intersections and tiling.
Perhaps the tikzDevice or the Cairo device could be adapted to allow
this? Ideally Grid's gpar() would be modified too, to pass a pattern
to devices that support it.

baptiste

baptiste auguie

unread,
Feb 15, 2011, 4:48:26 AM2/15/11
to ggplot2
One approach to make fill patterns with polygons would be to use grobX
and grobY. I haven't worked out the details for parallel lines at an
angle, but it seems doable. Other patterns than lines would be more
problematic, unless we restrict the pattern to strictly inner elements
(e.g. points), because their intersection with the polygon border
would be a nightmare.

Here's an example of usage for grobX, feel free to suggest adaptations
for parallel lines,

library(grid)

g <- polygonGrob(0.1*cos(seq(0,2*pi,length=300)) +0.5,
0.2*sin(seq(0,2*pi,length=300))+0.5, def="npc")


gtheta <- function(g, theta){

sapply(theta, function(.t){
gx <- convertX(grobX(g, .t), "npc")
gy <- convertY(grobY(g, .t), "npc")

c(gx,gy)
})

}

angles <- seq(10,360,by=10)
p <- gtheta(g, angles)

grid.newpage()
grid.draw(g)
grid.points(p[1,], p[2,], gp=gpar(cex=0.2),
default.units="npc")

grid.segments(0.5,0.5, p[1,],p[2,], gp=gpar(cex=0.2),
default.units="npc")


baptiste

martin_kaercher

unread,
Feb 20, 2011, 4:42:00 AM2/20/11
to ggplot2
Dear all,

I agree that colours look nicer than different shades of grey and I
also agree that the problem with colour blindness can easily be
avoided by checking for the right colours. However, there are some
good reasons why black and white patterns would be a huge improvement
for ggplot2. Almost a third (ca. 10 out of 33) of the geoms uses an
area that could be filled with a colour or a pattern. To publish a
nice coloured graph in a journal can cost up to several hundred Euros
per graph, which is not really cheap in my view. In some journals
coloured graphs can be published for free electronically but will be
printed in black and white if the authors don’t pay for the coloured
graphs. But even if graphs are published in colour electronically and
in print there still remains the problem that e.g. a librarian might
copy it in black and white at an article request which in the worst
case makes a graph useless. Different shades of grey are a good
alternative but only for a very limited number of colours/shades. The
most robust and basic solution for all these problems would be simple
black and white patterns. Only problem: they should not look stupid or
clumsy like in Excel but I’m sure there are some nice patterns
available. So if anyone could implement black and white patterns into
the next version of ggplot I would be really grateful and happy and I
think the same would apply to many other people as well.

Best wishes,

Martin


On 15 Feb., 10:48, baptiste auguie <bapt4...@googlemail.com> wrote:
> One approach to make fill patterns with polygons would be to use grobX
> and grobY. I haven't worked out the details for parallel lines at an
> angle, but it seems doable. Other patterns than lines would be more
> problematic, unless we restrict thepatternto strictly inner elements
> (e.g. points), because their intersection with the polygon border
> would be a nightmare.
>
> Here's an example of usage for grobX, feel free to suggest adaptations
> for parallel lines,
>
> library(grid)
>
> g <- polygonGrob(0.1*cos(seq(0,2*pi,length=300)) +0.5,
>                   0.2*sin(seq(0,2*pi,length=300))+0.5, def="npc")
>
> gtheta <- function(g, theta){
>
>   sapply(theta, function(.t){
>          gx <- convertX(grobX(g, .t), "npc")
>          gy <- convertY(grobY(g, .t), "npc")
>
>          c(gx,gy)
>        })
>
> }
>
> angles <- seq(10,360,by=10)
> p <- gtheta(g, angles)
>
> grid.newpage()
> grid.draw(g)
> grid.points(p[1,], p[2,], gp=gpar(cex=0.2),
>             default.units="npc")
>
> grid.segments(0.5,0.5, p[1,],p[2,], gp=gpar(cex=0.2),
>               default.units="npc")
>
> baptiste
>
> On Thu, Jan 20, 2011 at 9:38 AM, Brandon Hurr <bhiv...@gmail.com> wrote:
> > I could see it being useful for those dreaded dynamite plots, but also
> > geom_tile. If it could be made to work with geom_polygon & geom_area you
> > could use it topatternover an area of a map. I see its' main purpose as
> > making graph objects distinctive in the absence of color or over the top of
> > color. I would suggest that is perhaps it's sole purpose (primarily for
> > publication reasons), although I'm sure others could come up with other
> > reasons it could be useful to them.
> > B
>
> > On Thu, Jan 20, 2011 at 08:18, baptiste auguie <bapt4...@googlemail.com>
> > wrote:
>
> >> patternGrob is a grob already. What's really missing is some work to
> >> improve its implementation, and on the ggplot2 side a simple wrapper
> >> together with appropriate scales. The main question I have is, is it
> >> worth the trouble considering that it would be limited to filling
> >> rectangles?
>
> >> baptiste
>
> >> On Wed, Jan 19, 2011 at 6:45 PM, Brandon Hurr <bhiv...@gmail.com> wrote:
> >> > How close is patternGrob() to being a grid grob?
> >> > Does it need more patterns?
> >> > Brandon
>
> >> > On Wed, Jan 19, 2011 at 14:38, Hadley Wickham <had...@rice.edu> wrote:
>
> >> >> If anyone wanted to contribute this, the place to start would be
> >> >> writing a grid grob that supported it.   Then I could easily write the
> >> >> supporting infrastructure in ggplot2.
>
> >> >> Hadley
>
> >> >> On Wed, Jan 19, 2011 at 5:49 AM, baptiste auguie
> >> >> <bapt4...@googlemail.com> wrote:
> >> >> > I haven't found the motivation nor the time to give it a shot either.
> >> >> > With the new ggplot2(3?) infrastructure, perhaps this would make a
> >> >> > good exercise to see how it all works now (I don't know if it's
> >> >> > functional yet, though).
>
> >> >> > baptiste
>
> >> >> > On Tue, Jan 18, 2011 at 9:17 PM, Brandon Hurr <bhiv...@gmail.com>
> >> >> > wrote:
> >> >> >> I looked into it, but my knowledge of the ggplot subsystem quickly
> >> >> >> prevented
> >> >> >> me from understanding what I was doing and I stopped to work on
> >> >> >> other
> >> >> >> things. Sadly, that was as far as I got. I would have/still would
> >> >> >> gladly
> >> >> >> contribute to something if I can help. I'm mostly good at breaking
> >> >> >> things
> >> >> >> though.
> >> >> >> Brandon
>
> >> >> >> On Tue, Jan 18, 2011 at 19:56, Luciano Selzer
> >> >> >> <luciano.sel...@gmail.com>
> >> >> >> wrote:
>
> >> >> >>> Hello, just to know. Has there been any progress in writing a new
> >> >> >>> geom
> >> >> >>> that can use patterns?
> >> >> >>> Thank you
> >> >> >>> Luciano
>
> >> >> >>> 2010/9/13 baptiste Auguié <bapt4...@googlemail.com>
>
> >> >> >>>> you'll need a geom, I think, otherwise the output of your scale
> >> >> >>>> won't
> >> >> >>>> be
> >> >> >>>> turned into a graphical object; there is currently no geom that
> >> >> >>>> knows
> >> >> >>>> how to
> >> >> >>>> draw a patternGrob.
> >> >> >>>> baptiste
> >> >> >>>> On Sep 13, 2010, at 4:12 PM, Brandon Hurr wrote:
>
> >> >> >>>> Do you actually need a geom_*? Seems like what you really need is
> >> >> >>>> a
> >> >> >>>> scale_. Like a hacked version of scale_fill_color() that outputs
> >> >> >>>> patterns
> >> >> >>>> instead. Or are scales actually geom()s in hiding?
> >> >> >>>> Brandon
>
> >> >> >>>> On Mon, Sep 13, 2010 at 14:39, baptiste Auguié
> >> >> >>>> <bapt4...@googlemail.com>
> >> >> >>>> wrote:
>
> >> >> >>>>> On Sep 13, 2010, at 2:49 PM, Brandon Hurr wrote:
>
> >> >> >>>>> I've never actually looked at the guts of a geom_* before... how
> >> >> >>>>> much
> >> >> >>>>> motivation are we talking about?
>
> >> >> >>>>> Making a rough new geom is quite doable, the major steps are
> >> >> >>>>> summarized
> >> >> >>>>> here:
> >> >> >>>>>http://github.com/hadley/ggplot2/wiki/Creating-a-new-geom
> >> >> >>>>> However, making a *good* geom is another story :)
> >> >> >>>>> In the case ofpatternfills one would probably have to define
> >> >> >>>>> new
> >> >> >>>>> scales as well to map thepatterndensity and orientation.
> >> >> >>>>> baptiste
>
> >> >> >>>>> B
>
> >> >> >>>>> On Mon, Sep 13, 2010 at 13:25, baptiste Auguié
> >> >> >>>>> <bapt4...@googlemail.com>
> >> >> >>>>> wrote:
>
> >> >> >>>>>> Hi,
> >> >> >>>>>> I think it's still the official state of affairs.
> >> >> >>>>>> There's an experimental implementation of filling patterns for
> >> >> >>>>>> Grid
> >> >> >>>>>> graphics in the gridExtra package though, with some motivation
> >> >> >>>>>> one
> >> >> >>>>>> could
> >> >> >>>>>> write a ggplot2 geom for it.
> >> >> >>>>>> library(gridExtra)
> >> >> >>>>>> example(patternGrob)
> >> >> >>>>>> HTH,
> >> >> >>>>>> baptiste
> >> >> >>>>>> On Sep 13, 2010, at 2:18 PM, Brandon Hurr wrote:
>
> >> >> >>>>>> Last thing I could find about it says no.
> >> >> >>>>>> http://www.mail-archive.com/r-h...@r-project.org/msg74619.html
>
> >> >> >>>>>> Unfortunately grid (the underlying graphics library that ggplot2
> >> >> >>>>>> uses)
> >> >> >>>>>> does not currently support patterns.
>
> >> >> >>>>>> Hadley
>
> >> >> >>>>>> That was November 2009 though...
> >> >> >>>>>> Brandon
> >> >> >>>>>> On Mon, Sep 13, 2010 at 11:04, dM/ <david.n.mene...@gmail.com>
> >> >> >>>>>> wrote:
>
> >> >> >>>>>>> Hi
>
> >> >> >>>>>>> I'm new to the group, so sorry if this has been asked before.
> >> >> >>>>>>>  Is
> >> >> >>>>>>> it
> >> >> >>>>>>> possible to produce a histogram where the fill is not just a
> >> >> >>>>>>> solid
> >> >> >>>>>>> colour, but has apatternfill? (patternfill - e.g. red
> >> >> >>>>>>> diagonal
> >> >> >>>>>>> stripes on a white background)
>
> >> >> >>>>>>> I'm asking, because in the chart I'm working on, (using
> >> >> >>>>>>>scale_fill_manual) it can be hard to pick distinctive colours.
>
> >> >> >>>>>>> Also, if it is possible topatternfill, is there a way to
> >> >> >>>>>>> combine
> >> >> >>>>>>> it
> >> >> >>>>>>> efficiently with solid colours? e.g. I'd like to display four
> >> >> >>>>>>> bars
> >> >> >>>>>>> in
> >> >> >>>>>>> a chart, where
>
> >> >> >>>>>>> - bar 1 = "expected financial loss" - solid colour = red
> >> >> >>>>>>> - bar 2 = "actual financial loss"patternfill, colour = red
> >> >> >>>>>>> diag
> >> >> >>>>>>> strips on solid white background
> >> >> >>>>>>> - bar 3 = "expected borrowing costs" - solid colour = blue
> >> >> >>>>>>> - bar 4 = "expected borrowing costs"patternfill, colour =
> ...
>
> Erfahren Sie mehr »
Reply all
Reply to author
Forward
0 new messages