堆叠直方图在流式细胞术中呈R样 [英] stacked histograms in R -- like in flow cytometry

查看:553
本文介绍了堆叠直方图在流式细胞术中呈R样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot或普通的R来产生如下的内容:



据我所知,这不是一种显示数据的特别好方法 - 但是这个决定并不会停止与我。



一个样本数据集将如下所示:

  my.data < -  as.data.frame(rbind(cbind(rnorm(1e3),1),cbind(rnorm(1e3)+2,2),cbind(rnorm(1e3)+3,3),cbind rnorm(1e3)+4,4)))

我可以用geom_histogram绘制它,如下所示:

  ggplot(my.data)+ geom_histogram(aes(x = V1,fill = as.factor(V2)))+ facet_grid(V2〜。)

但是我想让y轴重叠。

解决方案

  require(ggplot2)
require(plyr)

my.data < - as.data.frame(rbind(cbind(rnorm(1e3),1),cbind(rnorm(1e3)+2,2),cbind(rnorm(1e3)+3,3),cbind (rnorm(1e3)+4,4)))
my.data $ V2 = as.factor(my.data $ V2)

计算d (V2),函数(x)密度(x),函数(x),密度函数(x) $ V1))
dd< - ldply(res,function(z){
data.frame(Values = z [[x]],
V1_density = z [[ y]],
V1_count = z [[y]] * z [[n]])
})

根据V2添加偏移量

  dd $ offest = -as .nu​​meric(dd $ V2)* 0.2#根据需要调整0.2值
dd $ V1_density_offest = dd $ V1_density + dd $fefst

和plot

  ggplot(dd,aes(Values,V1_density_offest,color = V2 ))+ 
geom_line()+
geom_ribbon(aes(Values,ymin = offest,ymax = V1_density_offest,fill = V2),alpha = 0.3)+
scale_y_continuous(breaks = NULL)


I'm trying to use ggplot or plain R to produce something like the following:

I know how to do histograms with ggplot2, and can easily separate them using facet_grid or facet_wrap. But I'd like to "stagger" them vertically, such that they have some overlap, as shown below. Sorry, I'm not allowed to post my own image, and it's quite difficult to find a simpler picture of what I want. If I could, I would only post the top-left panel.

I understand that this is not a particularly good way to display data -- but that decision does not rest with me.

A sample dataset would be as follows:

my.data <- as.data.frame(rbind( cbind( rnorm(1e3), 1) , cbind( rnorm(1e3)+2, 2), cbind( rnorm(1e3)+3, 3), cbind( rnorm(1e3)+4, 4)))

And I can plot it with geom_histogram as follows:

ggplot(my.data) + geom_histogram(aes(x=V1,fill=as.factor(V2))) + facet_grid( V2~.)

But I'd like the y-axes to overlap.

解决方案

require(ggplot2)
require(plyr)

my.data <- as.data.frame(rbind( cbind( rnorm(1e3), 1) , cbind(     rnorm(1e3)+2, 2), cbind( rnorm(1e3)+3, 3), cbind( rnorm(1e3)+4, 4)))
my.data$V2=as.factor(my.data$V2)

calculate the density depending on V2

res <- dlply(my.data, .(V2), function(x) density(x$V1))
dd <- ldply(res, function(z){
  data.frame(Values = z[["x"]], 
             V1_density = z[["y"]],
             V1_count = z[["y"]]*z[["n"]])
})

add an offset depending on V2

dd$offest=-as.numeric(dd$V2)*0.2 # adapt the 0.2 value as you need
dd$V1_density_offest=dd$V1_density+dd$offest

and plot

ggplot(dd, aes(Values, V1_density_offest, color=V2)) + 
  geom_line()+
  geom_ribbon(aes(Values, ymin=offest,ymax=V1_density_offest,     fill=V2),alpha=0.3)+
  scale_y_continuous(breaks=NULL)

这篇关于堆叠直方图在流式细胞术中呈R样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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