在 R 中连接的两侧豆图

Two sided bean plots with connection in R

我正在尝试在 R 中创建双面豆图。 我的数据如下:

> t1
   Country Women Kids
1    China     2    5
2    China     4   10
3    China     3   10
4    China     1    3
5    China     2    2
6      USA     1    1
7      USA     1    2
8      USA     2    1
9      USA     2    3
10     USA     1    0
11   Swiss     1    3
12   Swiss     2    6
13   Swiss     2    5
14   Swiss     1    2
15   Swiss     3    9

我使用 R 包尝试了以下 "beanplot":

> t2=melt(t1)
Using Country as id variables
> t2$C.M=paste(t2$Country,t2$variable,sep=" ")
> beanplot(value ~ C.M, data = t2, ll = 0.04,
+          main = NA, side = "both",ylab = "Count",
+          border = NA, col = list("blue", c("orange", "white")),what=c(1,1,1,1))

我得到了豆图: Bean plots for family structure per country

但是,我想要一个豆图来说明点对(即有孩子的女性)与每个国家/地区的联系之间的关系。它应该是这样的: This plot 但每个国家使用 two-sided 豆图而不是箱线图。

有办法实现吗?

你可以这样做:

library(beanplot)
library(reshape2)
library(beeswarm)

# melt
d1 <- melt(t1)
# draw the beans using the at to specify the positions, boxwex 
# to increase the size of the beans and xlim to increase the x-axis limits:

beanplot(d1$value ~ interaction(d1$variable, d1$Country), at=c(1.5,3.5,5.5), 
         side="both",col = list("blue", c("orange", "white")), what=c(1,1,1,1),
         boxwex=2, xlim=c(0,7))

# add the points
n <- beeswarm(d1$value ~ interaction(d1$variable, d1$Country), add=T, cex=2,
              pwcol =  d1$variable, pch=16)

# and finally the segments
segments(matrix(n$x,5,)[,1], d[1:5, 2], matrix(n$x,5,)[,2], d[1:5, 3], lwd= 2)
segments(matrix(n$x,5,)[,3], d[11:15, 2], matrix(n$x,5,)[,4], d[11:15, 3], lwd= 2)
segments(matrix(n$x,5,)[,5], d[6:10, 2], matrix(n$x,5,)[,6], d[6:10, 3], lwd= 2)