使用facet_wrap显示多个直方图

时间:2018-05-11 10:49:33

标签: r ggplot2 histogram 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)

enter image description here

如何在不同颜色的每个id的同一个图中显示x,y,z

1 个答案:

答案 0 :(得分:0)

最好先将数据从宽到长重新整形,然后添加fill美学来映射what(即xy,{ {1}})到不同的填充颜色:

z

enter image description here