Show multiple histogram using facet_wrap

别等时光非礼了梦想. 提交于 2019-12-25 03:06:01

问题


Sample data

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)

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


回答1:


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)



来源:https://stackoverflow.com/questions/50290657/show-multiple-histogram-using-facet-wrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!