[R] bar plot patterns

Paul Murrell p.murrell at auckland.ac.nz
Thu Jun 10 01:23:20 CEST 2004


Hi


Osman wrote:
 > Thank you for your answer. I have about 7 stacks it is not very
 > appealing to have just stripes with changing angles. I was wondering
 > if there is a way to have varying patters in black and white.


R graphics is inherently vector-based;  there is no native support for 
bitmaps so bitmap fill patterns are not really an option.  (There is the 
pixmap package for drawing bitmaps as a rectangle per pixel)

Of course, it is possible to do vector fill patterns, but again there is 
little convenient support for this sort of thing currently.  The code 
below shows one way that this sort of thing could be done.  It might be 
a useful starting point if your barplot is a one-off.

Paul

### R code ###

library(grid)

gridLocns <- function(sep=unit(0.25, "inches"),
                       startx=0.5*runif(1)*sep,
                       starty=0.5*runif(1)*vshift) {
   # hshift and vshift between rows of dots
   # Only really makes sense if sep is "absolute"
   if (round(convertWidth(sep, "inches", valueOnly=TRUE) -
             convertHeight(sep, "inches", valueOnly=TRUE), 5) > 0)
     stop("sep must be absolute")
   hshift <- sep*cos(pi*60/180)
   vshift <- sep*sin(pi*60/180)
   # How many sep steps to take in each direction in
   # order to guarantee to fill current viewport?
   nsepx <- 1/convertWidth(sep, "npc", valueOnly=TRUE) + 2
   nsepy <- 1/convertHeight(hshift, "npc", valueOnly=TRUE) + 2
   list(x=startx + rep(1:nsepx - 2, nsepy)*sep +
        rep(rep(0:1, each=nsepx), nsepy/2)*hshift,
        y=starty + rep(1:nsepy - 2, each=nsepx)*vshift)
}

dotFill <- function(size=unit(2, "mm"),
                     sep=unit(0.25, "inches")) {
   locns <- gridLocns(sep)
   grid.circle(locns$x, locns$y, 0.5*size, gp=gpar(fill="black"))
}

charFill <- function(char="+",
                      sep=unit(0.25, "inches")) {
   locns <- gridLocns(sep)
   grid.text(char, locns$x, locns$y, gp=gpar(fontsize=20))
}

N <- 6
counts <- runif(N)
fill <- c(dotFill, charFill)
pushViewport(plotViewport(c(5,4,4,2)))
pushViewport(viewport(xscale=c(0, N+1), yscale=c(0, max(counts)*1.1)))
# Draw a filled bar for each count
for (i in 1:N) {
   pushViewport(viewport(x=i, y=0, height=counts[i], width=0.5,
                         default.units="native",
                         just=c("centre", "bottom"),
                         clip=TRUE))
   fill[[i %% length(fill) + 1]]()
   grid.rect()
   popViewport()
}
grid.xaxis(at=1:N)
grid.yaxis()
grid.rect()
popViewport(2)

### R code ###


 > Osman ----- Original Message ----- From: Petr Pikal To: Osman Cc:
 > r-help at stat.math.ethz.ch Sent: Monday, May 31, 2004 5:01 AM Subject:
 > Re: [R] bar plot patterns
 >
 >
 >
 >
 >
 >
 > On 29 May 2004 at 16:54, Osman wrote:
 >
 >
 >> Dear R users,
 >>
 >> Is there a package or function that can produce multiple fill
 >> patterns to be used instead of colors in barplots or pie charts?
 >> Shades of grey are difficult to differentiate if more than 3 to 5..
 >>
 >
 >
 > Hi
 >
 >
 > This is what help page says about colours and shading lines
 >
 >
 > density: a vector giving the density of shading lines, in lines per
 > inch, for the bars or bar components. The default value of 'NULL'
 > means that no shading lines are drawn. Non-positive values of
 > 'density' also inhibit the drawing of shading lines.
 >
 >
 > angle: the slope of shading lines, given as an angle in degrees
 > (counter-clockwise), for the bars or bar components.
 >
 >
 > col: a vector of colors for the bars or bar components.
 >
 >
 >
 >
 >
 >
 > Do you want something else than
 >
 >
 > barplot(1:3, col=c(2,3,3),density=c(2,4,6), angle=c(30,60,90))
 >
 >
 > Cheers Petr
 >
 >
 >>
 >> Osman [[alternative HTML version deleted]]
 >>
 >> ______________________________________________
 >> R-help at stat.math.ethz.ch mailing list
 >> https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do
 >> read the posting guide! http://www.R-project.org/posting-guide.html
 >>
 >
 >
 >
 >
 > Petr Pikal petr.pikal at precheza.cz [[alternative HTML version
 > deleted]]
 >
 > ______________________________________________
 > R-help at stat.math.ethz.ch mailing list
 > https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read
 > the posting guide! http://www.R-project.org/posting-guide.html


-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/




More information about the R-help mailing list