8条回答 默认 最新
- ~Onlooker 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)
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')
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 RL+GNN解决人员排班问题时梯度消失
- ¥15 统计大规模图中的完全子图问题
- ¥15 使用LM2596制作降压电路,一个能运行,一个不能
- ¥60 要数控稳压电源测试数据
- ¥15 能帮我写下这个编程吗
- ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
- ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
- ¥20 @microsoft/fetch-event-source 流式响应问题
- ¥15 ogg dd trandata 报错
- ¥15 高缺失率数据如何选择填充方式