Filling bars in barplot with textiles rather than color in R

爷,独闯天下 提交于 2019-12-18 11:59:36

问题


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?


回答1:


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:

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

  2. 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") 



回答2:


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:



来源:https://stackoverflow.com/questions/21323798/filling-bars-in-barplot-with-textiles-rather-than-color-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!