How do I show striped patterns or any other texture for specific bars when using GGPLOT using example by BRIAN SHINE[Mathematial symbols in legend]

15,918 views
Skip to first unread message

Kasongo, Webster

unread,
Jan 3, 2013, 4:43:22 AM1/3/13
to ggp...@googlegroups.com

Dear All

How can one extend Brian Shine’s example if instead of using color for bar chart, I wanted to obtain bars with  stripped pattern instead of color.  I think Brian ‘s data can be used to illustrate this. I am  new to ggplot .

Webster

 

 

From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of Brian Shine
Sent: Wednesday, January 02, 2013 6:11 PM
To: Roman Luštrik
Cc: Brian Shine; ggp...@googlegroups.com
Subject: Re: Mathematical symbols in legends

 

Here are the data:

 

aghb <- data.frame(GHB = rep(1:4,3), Age = rep(1:3, each = 4), R = c(91.9, 79.1, 91.5, 104.6, 126.5, 144.6, 156.9, 187.8, 225.8, 257.3, 230.6, 241.2) )

 

and here's the code to produce bar chart.

 

labs <- list("<6.5%", "<7.5%", "<8.5%", expression("" >= 8.5))

labs2 <- list("<45", "<65", expression("" >= 65))

exp2 <- expression("" >= 8.5)

p1 <- ggplot(aghb, aes(x = factor(GHB), y = R, fill = factor(Age, labels = labs2))) + geom_bar(position = "dodge", stat = "identity") + labs(title = "Mortality rate by HbA1c and age in patients on renal replacement therapy", x = "HbA1c (%)", y = "Mortality / 1000 patient years", fill = "Age") 

p1 + scale_x_discrete(labels=labs)

 

That gives me the following:

 

ATT00001.txt
ATT00002.htm
PastedGraphic-1.pdf
PastedGraphic-2.pdf
ATT00003.htm
Example on page 44.pdf

Kasongo, Webster

unread,
Jan 3, 2013, 4:40:11 AM1/3/13
to ggp...@googlegroups.com
Example on page 44.pdf

Ben Bolker

unread,
Jan 3, 2013, 1:39:59 PM1/3/13
to ggp...@googlegroups.com
On 13-01-03 04:40 AM, Kasongo, Webster wrote:
> Dear All
>
> How can one extend Brian Shine�s example if instead of using color for
> bar chart, I wanted to obtain bars with stripped pattern instead of
> color. I think Brian �s data can be used to illustrate this. I am new
> to ggplot .
>
> Webster
>

I don't think this is possible in ggplot.

http://stackoverflow.com/questions/7604785/pattern-fills-for-graphs-in-r

Brandon Hurr

unread,
Jan 3, 2013, 1:46:45 PM1/3/13
to Ben Bolker, ggplot2
I concur. Currently not possible. The closest anyone has been is Baptiste's patternGrob mentioned in that SO question. The patterns are good, but the implementation isn't trivial and no one has taken the time because it's lower priority. 

If this is for a publication then consider using scale_fill_gray(). If you have too many shades of grey (say... 50), consider a different type of visualization. 

Brandon


On Thu, Jan 3, 2013 at 6:39 PM, Ben Bolker <bbo...@gmail.com> wrote:
On 13-01-03 04:40 AM, Kasongo, Webster wrote:
> Dear All
>
> How can one extend Brian Shine’s example if instead of using color for

> bar chart, I wanted to obtain bars with  stripped pattern instead of
> color.  I think Brian ‘s data can be used to illustrate this. I am  new
> to ggplot .
>
> Webster
>

   I don't think this is possible in ggplot.

http://stackoverflow.com/questions/7604785/pattern-fills-for-graphs-in-r

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

Greg Snow

unread,
Jan 3, 2013, 2:02:04 PM1/3/13
to Kasongo, Webster, ggp...@googlegroups.com
Using lines inside of bars or other geoms actually dates back to times before modern screens and printers when the only way to produce a high quality graphic was using a pen plotter.  These were machines that had a robotic arm that picked up a pen and drew the graph on the paper for you.  Because of that there was really only 1 line width available, wider lines were created by drawing the same line multiple times.  In that environment it made sense to not fill an entire rectangle, it was theoretically possible by drawing a bunch of lines really close to each other, but it was really slow (which irritated all the other people waiting to plot something) and often resulted in a hole in the piece of paper rather than something that looked nice.

With modern computers, screens, printers it is much easier to do full color fills so patterns are not really needed unless using a pen plotter.  I have not seen an actual pen plotter in many years, though our family does have a craft machine to cut things from paper/vinyl/etc. which can be converted to a pen plotter (part of me wants to write an R driver to create the old style plots with it, but I would need a lot more spare time and there would be no real benefit).

More recent research (see The Visual Display of Quantitative Information by Tufte, chapter 5) shows that using patterned lines is often more distracting than useful, often producing moire vibration and distorting the relative sizes of bars (poor color choices and other things can also distort relative sizes).  So, it is better not to use lines or hatching in statistical plots (unless your goal is to distort the data and distract the viewer from real information, but if that is the case don't expect us to help).

If color is not available to convey information (and even often when it is) then it may be better to use a different type of plot.  A dotplot may be more useful than a barplot for the example where you can use the point shape to convey information that would require pattern or color in a bar chart.


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



--
Gregory (Greg) L. Snow Ph.D.
538...@gmail.com

Ben Bolker

unread,
Jan 3, 2013, 2:10:56 PM1/3/13
to ggp...@googlegroups.com
On 13-01-03 02:02 PM, Greg Snow wrote:
> Using lines inside of bars or other geoms actually dates back to times
> before modern screens and printers when the only way to produce a high
> quality graphic was using a pen plotter. These were machines that had a
> robotic arm that picked up a pen and drew the graph on the paper for
> you. Because of that there was really only 1 line width available,
> wider lines were created by drawing the same line multiple times. In
> that environment it made sense to not fill an entire rectangle, it was
> theoretically possible by drawing a bunch of lines really close to each
> other, but it was really slow (which irritated all the other people
> waiting to plot something) and often resulted in a hole in the piece of
> paper rather than something that looked nice.
>

On the other hand, I could entertain myself for far too long watching
the plotter do its thing -- put down one color pen, draw a few lines,
pick up another pen ...

> With modern computers, screens, printers it is much easier to do full
> color fills so patterns are not really needed unless using a pen
> plotter. I have not seen an actual pen plotter in many years, though
> our family does have a craft machine to cut things from paper/vinyl/etc.
> which can be converted to a pen plotter (part of me wants to write an R
> driver to create the old style plots with it, but I would need a lot
> more spare time and there would be no real benefit).
>
> More recent research (see The Visual Display of Quantitative Information
> by Tufte, chapter 5) shows that using patterned lines is often more
> distracting than useful, often producing moire vibration and distorting
> the relative sizes of bars (poor color choices and other things can also
> distort relative sizes). So, it is better not to use lines or hatching
> in statistical plots (unless your goal is to distort the data and
> distract the viewer from real information, but if that is the case don't
> expect us to help).
>
> If color is not available to convey information (and even often when it
> is) then it may be better to use a different type of plot. A dotplot
> may be more useful than a barplot for the example where you can use the
> point shape to convey information that would require pattern or color in
> a bar chart.

There are times when I have wished for pattern fills, for example to
differentiate a couple of different categorical variables (fill color
for one, fill pattern for the other, e.g. species={red,blue},
treatment={open,hashed}.


>
> On Thu, Jan 3, 2013 at 2:40 AM, Kasongo, Webster
> <webster...@vanderbilt.edu <mailto:webster...@vanderbilt.edu>>
> wrote:
>
> Dear All____
>
> How can one extend Brian Shine�s example if instead of using color
> for bar chart, I wanted to obtain bars with stripped pattern
> instead of color. I think Brian �s data can be used to illustrate
> this. I am new to ggplot .____
>
> Webster____
>
> --
> 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
> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>
> More options: http://groups.google.com/group/ggplot2
>
>
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> 538...@gmail.com <mailto:538...@gmail.com>

Kasongo, Webster

unread,
Jan 3, 2013, 2:10:56 PM1/3/13
to ggp...@googlegroups.com

Good to know it is not possible in ggplot and thanks for the history lesson from Dr.Greg Snow.

Webster

Boo Radley

unread,
Dec 6, 2013, 9:36:45 AM12/6/13
to ggp...@googlegroups.com, webster...@vanderbilt.edu
Here is a wee hack I came up with using Troy's initial solution to highlighting borders over at StackOverflow:

http://stackoverflow.com/questions/20334997/ggplot2-make-the-border-on-one-bar-darker-than-the-others-using-r

Hope this helps.

Boo Radley

unread,
Nov 9, 2014, 8:42:14 PM11/9/14
to ggp...@googlegroups.com, webster...@vanderbilt.edu
Hi folks,

I got some time and properly put together an answer over on Stack Overflow regarding approaches to adding textures/patterns to bar graphs in ggplot. The suggestions can be found here:

http://stackoverflow.com/a/20426482/2653210

Reply all
Reply to author
Forward
0 new messages