用纺织品填充条形图中的条形,而不是用R填充颜色 [英] Filling bars in barplot with textiles rather than color in R

查看:88
本文介绍了用纺织品填充条形图中的条形,而不是用R填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不同的纺织品而不是颜色在条形图中填充条形图.

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

这会给我这样的东西:

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

此刻,我正在使用igraph库,并且不确定是否还有其他软件包可以使我使用其他类型的填充.

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.

有什么建议吗?

推荐答案

plotrix 软件包提供了 rectFill barp 功能,可以用自定义形状填充条形图.

plotrix package provides rectFill and barp functions, which allow to fill the bars with custom shapes.

barp生成条形图,并调用rectFill为其填充符号.可以使用默认的pch参数(参考图)来指定符号,也可以通过自定义字符串,请参见下面的示例.

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("*","$","~","@")))

不幸的是,barp不太灵活:

  1. 必须以矩阵格式提供数据以指定变化的符号.矩阵的列应与pch参数的序列相对应.因此,在当前示例中需要t().

一旦指定了pch,绘图将变为黑白. rectFill功能允许通过pch.col控制符号的颜色,但是barp不允许此选项.为了解决这个问题,我在barp源代码中添加了省略号(...),以便能够将其他参数传递给rectFill函数.修改后的代码(barp2)在此处.

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)

编辑:该软件包的最新版本似乎存在错误,因为某些用户在重绘图表时遇到了麻烦.如果发生这种情况,请尝试使用以下代码安装plotrix的早期版本:

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

这篇关于用纺织品填充条形图中的条形,而不是用R填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆