11

我只是通过以下情节来的:

替代文字

并想知道如何在 R 中完成它?(或其他软件)

10.03.11 更新:感谢所有参与回答此问题的人 - 您提供了精彩的解决方案!我已经在我的博客上的一篇文章中编译了这里提供的所有解决方案(以及我在网上找到的其他一些解决方案)。

4

5 回答 5

9

Make.Funny.Plot 或多或少做了我认为它应该做的事情。根据您自己的需要进行调整,并且可能会进行一些优化,但这应该是一个不错的开始。

Make.Funny.Plot <- function(x){
    unique.vals <- length(unique(x))
    N <- length(x)
    N.val <- min(N/20,unique.vals)

    if(unique.vals>N.val){
      x <- ave(x,cut(x,N.val),FUN=min)
      x <- signif(x,4)
    }
    # construct the outline of the plot
    outline <- as.vector(table(x))
    outline <- outline/max(outline)

    # determine some correction to make the V shape,
    # based on the range
    y.corr <- diff(range(x))*0.05

    # Get the unique values
    yval <- sort(unique(x))

    plot(c(-1,1),c(min(yval),max(yval)),
        type="n",xaxt="n",xlab="")

    for(i in 1:length(yval)){
        n <- sum(x==yval[i])
        x.plot <- seq(-outline[i],outline[i],length=n)
        y.plot <- yval[i]+abs(x.plot)*y.corr
        points(x.plot,y.plot,pch=19,cex=0.5)
    }
}

N <- 500
x <- rpois(N,4)+abs(rnorm(N))
Make.Funny.Plot(x)

编辑:已更正,因此它始终有效。

于 2010-09-02T12:46:29.553 回答
8

我最近遇到了 beeswarm 包,它有一些相似之处。

蜂群图是类似于“条形图”的一维散点图,但具有紧密排列的非重叠点。

这是一个例子:

  library(beeswarm)
  beeswarm(time_survival ~ event_survival, data = breast,
    method = 'smile',
    pch = 16, pwcol = as.numeric(ER),
    xlab = '', ylab = 'Follow-up time (months)',
    labels = c('Censored', 'Metastasis'))
  legend('topright', legend = levels(breast$ER),
    title = 'ER', pch = 16, col = 1:2)


(来源:eklund,网址:www.cbs.dtu.dk

于 2010-10-14T14:55:28.180 回答
4

我想出了类似于 Joris 的代码,但我认为这不仅仅是一个梗图;这里我的意思是,每个系列中的 y 值是与 bin 均值的距离的绝对值,x 值更多的是关于该值是低于还是高于平均值。
示例代码(有时会引发警告但有效):

px<-function(x,N=40,...){
x<-sort(x);

#Cutting in bins
cut(x,N)->p;

#Calculate the means over bins
sapply(levels(p),function(i) mean(x[p==i]))->meansl;
means<-meansl[p];

#Calculate the mins over bins
sapply(levels(p),function(i) min(x[p==i]))->minl;
mins<-minl[p];

#Each dot is one value.
#X is an order of a value inside bin, moved so that the values lower than bin mean go below 0
X<-rep(0,length(x));
for(e in levels(p)) X[p==e]<-(1:sum(p==e))-1-sum((x-means)[p==e]<0);
#Y is a bin minum + absolute value of a difference between value and its bin mean
plot(X,mins+abs(x-means),pch=19,cex=0.5,...);
}
于 2010-09-02T14:05:11.540 回答
2

试试vioplot包:

library(vioplot)
vioplot(rnorm(100))

(使用糟糕的默认颜色 ;-)

wvioplot包中还有 wvioplot() ,用于加权小提琴图和beanplot,它结合了小提琴和地毯图。它们也可以通过lattice包获得,请参阅?panel.violin

于 2010-09-01T13:54:11.347 回答
2

由于尚未提及,因此还有ggbeeswarm作为基于 ggplot2 的相对较新的 R 包。

这为 ggplot 添加了另一个 geom 以代替 geom_jitter 等。

特别是geom_quasirandom(见下面的第二个例子)产生了非常好的结果,事实上我已经将它改编为默认图。

值得注意的是包vipor(R 中的 VIolin POints),它使用标准 R 图形生成绘图,实际上 ggbeeswarm 在幕后也使用它。


set.seed(12345)
install.packages('ggbeeswarm')
library(ggplot2)
library(ggbeeswarm)

ggplot(iris,aes(Species, Sepal.Length)) + geom_beeswarm()

ggplot(iris,aes(Species, Sepal.Length)) + geom_quasirandom()

#compare to jitter
ggplot(iris,aes(Species, Sepal.Length)) + geom_jitter()

于 2017-06-14T16:47:47.673 回答