由于变量,框内的R着色箱图(堆积箱图)

时间:2016-08-03 14:22:11

标签: r colors boxplot stacked

我想知道如何在盒子中创建两种不同颜色的箱形图。 例如我的变量d,我正在进行的是变量b和c的总和。因此,在每个框内,颜色可以指示变量b和c的比例,从而创建d。 我希望这是可以理解的。

以下是我的例子:

    a<-c("A","A","B","B","B","C","C","C","B","A")
    b<-c(1,2,3,4,3,4,5,6,3,4)
    c<-c(5,6,4,5,2,1,2,1,5,8)
    d<-c(6,8,7,9,5,5,7,7,8,12)
    df<-data.frame(a,b,c,d)

    boxplot(d~a)

现在我想根据变量b和c为每个方框着色,以便显示比例。

这是一张显示用Excel制作的图表的图片。 example http://www.real-statistics.com/wp-content/uploads/2012/11/box-plot-excel.png

您有什么想法如何实现这一目标? 谢谢!

2 个答案:

答案 0 :(得分:1)

您可以尝试:

# First the boxplot
n <- boxplot(d ~ a)
# check the x values for the boxes, here it is for A 0.6 and 1.4
axis(1, seq(0, 5, 0.1))

# proportions for the b values depended on a

# the mean values calculated using another approach you mentioned in the comment
ratio <- aggregate(df[ , -1], list(df$a), mean)
# get the percentages
ratio <- ratio$b/ratio$d

# your approach:
ratio <- c(by(df, INDICES = df$a, FUN = function(x) mean(x$b/x$d)))
ratio    
A         B         C 
0.2500000 0.4620040 0.7904762

# caculate the y values for the rectangles, no matter which mean-calculation method you used
low <- diff(n$stats[c(2, 4), ])*ratio
high <- diff(n$stats[c(2, 4),])*(1-ratio)

# the final plot
n <- boxplot(d ~ a)
rect(xleft = c(0.6) + seq_along(n$n)-1, xright = 1.4 + seq_along(n$n)-1, ybottom = n$stats[2, ], ytop = n$stats[2, ]+low, col = rgb(1, 1,0 ,0.4))
rect(xleft = c(0.6) + seq_along(n$n)-1, xright = 1.4 + seq_along(n$n)-1, ybottom = n$stats[4, ], ytop = n$stats[4, ]-high, col = rgb(0, 1, 1, 0.4))

我们的想法是使用rect()将矩形绘制到框中。您必须分别为start和end提供x和y值。通过使用axis添加更多连续的x轴,您可以轻松地从箱图中读取x值。 与b相比,y值取决于cd比例。因此,您使用baggregate计算一个向量(此处为by)的比率,并在y内生成rect()值。最后,rgb()函数计算为透明度添加alpha参数的颜色。

enter image description here

答案 1 :(得分:0)

You can do a pie chart to show the share of vectors b and c in d (cf. image in link)

下面的代码显示了如何执行此操作:

c_share = sum(c)/sum(d)
b_share = sum(b)/sum(d)
mat = cbind(c_share, b_share)
pie(mat, labels=c("Share of C", "Share of B"))