If you combine the tidyr
and ggplot2
packages, you can use facet_wrap
to make a quick set of histograms of each variable in your data.frame.
You need to reshape your data to long form with tidyr::gather
, so you have key
and value
columns like such:
library(tidyr)
library(ggplot2)
# or `library(tidyverse)`
mtcars %>% gather() %>% head()
#> key value
#> 1 mpg 21.0
#> 2 mpg 21.0
#> 3 mpg 22.8
#> 4 mpg 21.4
#> 5 mpg 18.7
#> 6 mpg 18.1
Using this as our data, we can map value
as our x variable, and use facet_wrap
to separate by the key
column:
ggplot(gather(mtcars), aes(value)) +
geom_histogram(bins = 10) +
facet_wrap(~key, scales = 'free_x')
The scales = 'free_x'
is necessary unless your data is all of a similar scale.
You can replace bins = 10
with anything that evaluates to a number, which may allow you to set them somewhat individually with some creativity. Alternatively, you can set binwidth
, which may be more practical, depending on what your data looks like. Regardless, binning will take some finesse.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…