多个直方图相互叠加,没有垃圾箱

发布于 2024-11-07 22:43:00 字数 487 浏览 3 评论 0原文

假设我有这个有 2 个级别的数据框。 LC和HC。 现在我想要两个像下面这样相互叠加的图。

data <- data.frame(
    welltype=c("LC","LC","LC","LC","LC","HC","HC","HC","HC","HC"),
    value=c(1,2,1,2,1,5,4,5,4,5))

获得以下情节的代码=

x <- rnorm(1000)
y <- hist(x)
plot(y$breaks,
   c(y$counts,0),
   type="s",col="blue")

(感谢Joris Meys)

那么,我该如何开始呢?因为我习惯了java,所以我想到了for循环,但我被告知不要这样做。

在此处输入图像描述

Let's say I've got this dataframe with 2 levels. LC and HC.
Now i want to get 2 plots like below on top of eachother.

data <- data.frame(
    welltype=c("LC","LC","LC","LC","LC","HC","HC","HC","HC","HC"),
    value=c(1,2,1,2,1,5,4,5,4,5))

The code to get following plot =

x <- rnorm(1000)
y <- hist(x)
plot(y$breaks,
   c(y$counts,0),
   type="s",col="blue")

(with thanks to Joris Meys)

So, how do I even start on this. Since I'm used to java I was thinking of a for loop, but I've been told not to do it this way.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2024-11-14 22:43:00

除了 Aaron 提供的方法之外,还有一个 ggplot 解决方案(见下文),
但我强烈建议您使用密度,因为它们会给出更好的图并且更容易构造:

# make data
wells <- c("LC","HC","BC")
Data <- data.frame(
    welltype=rep(wells,each=100),
    value=c(rnorm(100),rnorm(100,2),rnorm(100,3))
)

ggplot(Data,aes(value,fill=welltype)) + geom_density(alpha=0.2)

给出 :在此处输入图像描述

对于您请求的绘图:

# make hists dataframe
hists <- tapply(Data$value,Data$welltype,
            function(i){
              tmp <- hist(i)
              data.frame(br=tmp$breaks,co=c(tmp$counts,0))
            })
ll <- sapply(hists,nrow)
hists <- do.call(rbind,hists)
hists$fac <- rep(wells,ll)

# make plot
require(ggplot2)
qplot(br,co,data=hists,geom="step",colour=fac)

在此处输入图像描述

Next to the method provided by Aaron, there's a ggplot solution as well (see below),
but I would strongly advise you to use the densities, as they will give nicer plots and are a whole lot easier to construct :

# make data
wells <- c("LC","HC","BC")
Data <- data.frame(
    welltype=rep(wells,each=100),
    value=c(rnorm(100),rnorm(100,2),rnorm(100,3))
)

ggplot(Data,aes(value,fill=welltype)) + geom_density(alpha=0.2)

gives :enter image description here

For the plot you requested :

# make hists dataframe
hists <- tapply(Data$value,Data$welltype,
            function(i){
              tmp <- hist(i)
              data.frame(br=tmp$breaks,co=c(tmp$counts,0))
            })
ll <- sapply(hists,nrow)
hists <- do.call(rbind,hists)
hists$fac <- rep(wells,ll)

# make plot
require(ggplot2)
qplot(br,co,data=hists,geom="step",colour=fac)

enter image description here

花心好男孩 2024-11-14 22:43:00

您可以使用相同的代码,但使用点而不是图来向图中添加其他线。

制作一些数据

set.seed(5)
d <- data.frame(x=c(rnorm(1000)+3, rnorm(1000)),
                g=rep(1:2, each=1000) )

并以相当简单的方式进行:

x1 <- d$x[d$g==1]
x2 <- d$x[d$g==2]
y1 <- hist(x1, plot=FALSE)
y2 <- hist(x2, plot=FALSE)
plot(y1$breaks, c(y1$counts,0), type="s",col="blue",
     xlim=range(c(y1$breaks, y2$breaks)), ylim=range(c(0,y1$counts, y2$counts)))
points(y2$breaks, c(y2$counts,0), type="s", col="red")

或者以更 R 式的方式:

col <- c("blue", "red")
ds <- split(d$x, d$g)
hs <- lapply(ds, hist, plot=FALSE)
plot(0,0,type="n",
     ylim=range(c(0,unlist(lapply(hs, function(x) x$counts)))),
     xlim=range(unlist(lapply(hs, function(x) x$breaks))) )
for(i in seq_along(hs)) {
  points(hs[[i]]$breaks, c(hs[[i]]$counts,0), type="s", col=col[i])
}

编辑:受到 Joris 答案的启发,我会注意到网格也可以轻松地绘制重叠密度图。

library(lattice)
densityplot(~x, group=g, data=d)

You can use the same code except with points instead of plot for adding additional lines to the plot.

Making up some data

set.seed(5)
d <- data.frame(x=c(rnorm(1000)+3, rnorm(1000)),
                g=rep(1:2, each=1000) )

And doing it in a fairly straightforward way:

x1 <- d$x[d$g==1]
x2 <- d$x[d$g==2]
y1 <- hist(x1, plot=FALSE)
y2 <- hist(x2, plot=FALSE)
plot(y1$breaks, c(y1$counts,0), type="s",col="blue",
     xlim=range(c(y1$breaks, y2$breaks)), ylim=range(c(0,y1$counts, y2$counts)))
points(y2$breaks, c(y2$counts,0), type="s", col="red")

Or in a more R-ish way:

col <- c("blue", "red")
ds <- split(d$x, d$g)
hs <- lapply(ds, hist, plot=FALSE)
plot(0,0,type="n",
     ylim=range(c(0,unlist(lapply(hs, function(x) x$counts)))),
     xlim=range(unlist(lapply(hs, function(x) x$breaks))) )
for(i in seq_along(hs)) {
  points(hs[[i]]$breaks, c(hs[[i]]$counts,0), type="s", col=col[i])
}

EDIT: Inspired by Joris's answer, I'll note that lattice can also easily do overlapping density plots.

library(lattice)
densityplot(~x, group=g, data=d)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文