I am trying to fill bars in a barplot using different textiles rather than color.
I know how to fill the bars with different patterns of lines like this:
aaa <- c(1,2,3,4)
barplot(aaa, density = c(4,4,4,4), angle = c(45, 90, 180, -45))
This will give me something like this:
___
4| |\\\|
| ___ |\\\|
3| |___| |\\\|
| |___| |\\\|
2| ___ |___| |\\\|
| ||||| |___| |\\\|
1|___ ||||| |___| |\\\|
|///| ||||| |___| |\\\|
0|///|__|||||__|___|__|\\\|_
1 2 3 4
I am not sure how to do this with other types of fills (for example shapes such as *, squares, colored dots, triangles, etc). Or if this can actually be done at all.
At the moment, I am using the igraph library, and am not too sure if there are any other packages that may allow me to use other types of fills.
Any suggestions?
plotrix
package provides rectFill
and barp
functions, which allow to fill the bars with custom shapes.
barp
produces barplots and calls rectFill
to fill them with symbols. The symbols can be specified using default pch
parameters (reference chart), or via custom string, see the examples below.
# install the package
install.packages('plotrix')
# examples with varying pch parameters
require(plotrix)
a <- 1:4
barp(t(a), pch=t(1:4))
barp(t(a), pch=t(c("*","$","~","@")))
Unfortunately barp
is not very flexible:
The data has to be supplied in a matrix format to specify varying symbols. The columns of the matrix should correspond with the sequence of pch parameters. Hence the need for t()
in the current examples.
Once pch
is specified, the plot becomes black and white. rectFill
function allows to control the colour of the symbols via pch.col
, but the barp
doesn't allow this option. To address this, I added the ellipsis (...
) to the barp
source code to be able to pass further arguments to the rectFill
function. The modified code (barp2
) is available here.
Example using modified code:
# load the barp2 function
require(devtools)
source_gist("https://gist.github.com/tleja/8592929")
# run the barp2 function
require(plotrix)
a <- 1:4
barp2(t(a), pch=t(1:4), pch.col=1:4)
EDIT: It appears that the most recent version of the package may have a bug, since some users are having trouble reproducing the plots. If that happens, please try installing the earlier version of plotrix
using the code below:
require(devtools)
install_url("http://cran.r-project.org/src/contrib/Archive/plotrix/plotrix_3.5-2.tar.gz")
I'm sure something like below can be adapted:
aaa <- c(1,2,3,4)
bp <- barplot(aaa,col="white")
fillbars <- function(height,bp,bg,size,spacing) {
coords <- data.frame(bp-0.5,bp+0.5,0,height)
invisible(
apply(
coords,
1,
function(d) {
do.call(clip,unname(as.list(d)))
x <- seq(d[1],d[2],by=spacing)
y <- seq(d[3],d[4],by=spacing)
xy <- expand.grid(x,y)
symbols(
xy,
circles=rep(size,nrow(xy)),
inches=FALSE,
bg=bg,
add=TRUE
)
}
)
)
}
fillbars(height=aaa,bp=bp,bg="red",size=0.06,spacing=0.3)
Result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With