谁还没个明天 2010-08-22 13:54 采纳率: 50%
浏览 201
已采纳

如何在 r 中绘制两个直方图?

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:

overlapped density

转载于:https://stackoverflow.com/questions/3541713/how-to-plot-two-histograms-together-in-r

  • 写回答

8条回答 默认 最新

  • 7*4 2010-08-22 15:51
    关注

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

    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 formal already, you only need one line to make your plot.

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

    enter image description here

    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')
    

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?