如何在R中一起绘制两个直方图? [英] How to plot two histograms together in R?

查看:1483
本文介绍了如何在R中一起绘制两个直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R,我有两个数据框:胡萝卜和黄瓜.每个数据框都有一个数字列,其中列出了所有测得的胡萝卜(总计:100k胡萝卜)和黄瓜(总计:50k黄瓜)的长度.

I am using R and I have two data frames: carrots and cucumbers. Each data frame has a single numeric column which lists the length of all measured carrots (total: 100k carrots) and cucumbers (total: 50k cucumbers).

我希望在同一图上绘制两个直方图-胡萝卜长度和黄瓜长度.它们重叠,所以我想我也需要一些透明度.我还需要使用相对频率而不是绝对数字,因为每个组中的实例数量是不同的.

I wish to plot two histogram - carrot length and cucumbers lengths - on the same plot. They overlap, so I guess I also need some transparency. I also need to use relative frequencies not absolute numbers since the number of instances in each group is different.

类似这样的东西会很好,但是我不明白如何从我的两个表中创建它:

something like this would be nice but I don't understand how to create it from my two tables:

推荐答案

您链接到的图像用于密度曲线,而不是直方图.

That image you linked to was for density curves, not histograms.

如果您一直在阅读ggplot,那么也许您唯一缺少的就是将两个数据帧组合成一个长帧.

If you've been reading on ggplot then maybe the only thing you're missing is combining your two data frames into one long one.

所以,让我们从拥有的东西开始,将两组独立的数据合并起来.

So, let's start with something like what you have, two separate sets of data and combine them.

carrots <- data.frame(length = rnorm(100000, 6, 2))
cukes <- data.frame(length = rnorm(50000, 7, 2.5))

# Now, combine your two dataframes into one.  
# First make a new column in each that will be 
# a variable to identify where they came from later.
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'

# and combine into your new data frame vegLengths
vegLengths <- rbind(carrots, cukes)

此后,如果您的数据已经是长格式,则不需要此操作,只需一行即可制作图.

After that, which is unnecessary if your data is in long format already, you only need one line to make your plot.

ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.2)

现在,如果您确实想要直方图,则可以使用以下方法.请注意,您必须从默认的堆栈"参数更改位置.如果您真的不了解数据的外观,那么您可能会想念它.较高的alpha值在那里看起来更好.另请注意,我将其设为密度直方图.删除y = ..density..使其恢复计数很容易.

Now, if you really did want histograms the following will work. Note that you must change position from the default "stack" argument. You might miss that if you don't really have an idea of what your data should look like. A higher alpha looks better there. Also note that I made it density histograms. It's easy to remove the y = ..density.. to get it back to counts.

ggplot(vegLengths, aes(length, fill = veg)) + 
   geom_histogram(alpha = 0.5, aes(y = ..density..), position = 'identity')

这篇关于如何在R中一起绘制两个直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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