使用facet_wrap显示多个直方图 [英] Show multiple histogram using facet_wrap

查看:120
本文介绍了使用facet_wrap显示多个直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

样本数据

df <- data.frame(id = rep(1:6, each = 50), x = rnorm(50*6, mean = 10, sd = 5), 
                                       y = rnorm(50*6, mean = 20, sd = 10), 
                                       z = rnorm(50*6, mean = 30, sd = 15))

ggplot(df, aes(x)) + geom_histogram() + facet_wrap(~id)

对于相同ID的不同颜色,如何在同一图中显示x,y,z

How do I show x, y, z in the same plot for each id in different colours

推荐答案

最好先将数据从宽变长到长整形,然后在映射what时添加fill美观度(即xyz)设置为不同的填充颜色:

It's best to reshape data from wide to long first, and then add a fill aesthetic to map what (i.e. x, y, z) to different fill colours:

library(tidyverse)
df %>%
    gather(what, val, -id) %>%
    ggplot(aes(val, fill = what)) + geom_histogram() + facet_wrap(~id)

这篇关于使用facet_wrap显示多个直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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