彩色填充盒在R型起重机中的线,点或类似物 [英] Colorfill boxplot in R-cran with lines, dots, or similar

查看:724
本文介绍了彩色填充盒在R型起重机中的线,点或类似物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用黑白颜色为我的盒图在R.我想彩色填充盒线图与线和点。例如:





我想象 ggplot2 可以做到,但我找不到任何办法。



感谢您的帮助!

解决方案

很难用 ggplot2 这样做,因为它不使用阴影多边形(gris limitatipn)。但您可以在基本图中使用阴影线要素,以某些绘图函数(ploygon,barplot,..)中的密度



boxplot 的问题不使用此功能。所以我黑客,或者我hack bxp 在boxplot内部使用。该攻击包括在bxp函数中添加2个参数(角度和密度),并在 xypolygon 函数的调用中内部添加它们(这发生在2行)。

  my.bxp<  -  function(all.bxp.argument,angle,density,...){
... .. #### bxp code
xypolygon(xx,yy,lty = boxlty [i],lwd = boxlwd [i],
border = boxcol [i],angle [i] (xx,yy,lty =blank,col = boxfill [i],angle [i],density [i]),
])
......

}

这里举个例子。应该注意的是,用户完全有责任确保
图例对应于情节。因此,我添加了一些代码来重新排列图例框代码。



  require(stats)
set.seed(753)$ b $布局(矩阵(c(1,2),nrow = 1),$ b(5),b(bx.p←boxplot(split(rt(100,4),gl(5,20) $ b width = c(4,1))
angles = c(60,30,40,50,60)
densities = c(50,30,40,50,30)
par(mar = c(5,4,4,0))#除去右边的边距
my.bxp(bx.p,angle = angles,density = densities)
左边的边距
(c(0,1),type =n,axes = F,xlab = ylab =)
legend(top,paste(region,1:5),
angle = angles,density = densities)
/ pre>

I need to use black and white color for my boxplots in R. I would like to colorfill the boxplot with lines and dots. For an example:

I imagine ggplot2 could do that but I can't find any way to do it.

Thank you in advance for your help!

解决方案

I think it is hard to do this with ggplot2 since it dont use shading polygon(gris limitatipn). But you can use shading line feature in base plot, paramtered by density and angle arguments in some plot functions ( ploygon, barplot,..).

The problem that boxplot don't use this feature. So I hack it , or rather I hack bxp internally used by boxplot. The hack consist in adding 2 arguments (angle and density) to bxp function and add them internally in the call of xypolygon function ( This occurs in 2 lines).

my.bxp <- function (all.bxp.argument,angle,density, ...) {
    .....#### bxp code
    xypolygon(xx, yy, lty = boxlty[i], lwd = boxlwd[i], 
    border = boxcol[i],angle[i],density[i])  
    .......## bxp code after
    xypolygon(xx, yy, lty = "blank", col = boxfill[i],angle[i],density[i])      
    ......

}

Here an example. It should be noted that it is entirely the responsibility of the user to ensure that the legend corresponds to the plot. So I add some code to rearrange the legend an the boxplot code.

require(stats)
set.seed(753)
(bx.p <- boxplot(split(rt(100, 4), gl(5, 20))))
layout(matrix(c(1,2),nrow=1),
       width=c(4,1)) 
angles=c(60,30,40,50,60)
densities=c(50,30,40,50,30)
par(mar=c(5,4,4,0)) #Get rid of the margin on the right side
my.bxp(bx.p,angle=angles,density=densities)
par(mar=c(5,0,4,2)) #No margin on the left side
plot(c(0,1),type="n", axes=F, xlab="", ylab="")
legend("top", paste("region", 1:5),
       angle=angles,density=densities)

这篇关于彩色填充盒在R型起重机中的线,点或类似物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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