多个直方图相互叠加,没有垃圾箱
假设我有这个有 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 Aaron 提供的方法之外,还有一个 ggplot 解决方案(见下文),
但我强烈建议您使用密度,因为它们会给出更好的图并且更容易构造:
给出 :
对于您请求的绘图:
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 :
gives :
For the plot you requested :
您可以使用相同的代码,但使用点而不是图来向图中添加其他线。
制作一些数据
并以相当简单的方式进行:
或者以更 R 式的方式:
编辑:受到 Joris 答案的启发,我会注意到网格也可以轻松地绘制重叠密度图。
You can use the same code except with points instead of plot for adding additional lines to the plot.
Making up some data
And doing it in a fairly straightforward way:
Or in a more R-ish way:
EDIT: Inspired by Joris's answer, I'll note that lattice can also easily do overlapping density plots.