Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot histograms over factor variables

Tags:

r

ggplot2

I'm trying to replicate the graph similar to the following (originally found HERE)
stacked histograms

It's conceptually simple, but I'm a bit stumped as to how to do it in R.
To summarize: I want to generate histograms of behavioral frequency over the 24 hours of the day (24-level factor variable) by each day of the week. Then, I want to stack these histograms on top of each other so that the distribution of behavior over the hour of day can easily be compared (again, see example).

For example, my data might look like this:

  weekday hour    count
  Tuesday   15      553
   Monday    1       53
   Monday   10      539
   Monday   15      629
  Tuesday    9      281
 Saturday    4       11
   Monday    3       20
   Sunday    3       10
   Sunday    7        2
   Sunday    2       17

How do I go about implementing the graph in the link above? I'm terrible with ggplot, but have a sense it's the likely solution. Thanks!

like image 944
roody Avatar asked Feb 11 '13 18:02

roody


People also ask

Can histograms be used for categorical variables?

A histogram can be used to show either continuous or categorical data in a bar graph.

Can you make a histogram with 2 variables?

In this method, to create a histogram of two variables, the user has to first install and import the ggplot2 package, and then call the geom_histrogram with the specified parameters as per the requirements and needs to create the dataframe with the variable to which we need the histogram in the R programming language.


1 Answers

Something like this?

set.seed(1234)
df <- data.frame(weekday=rep(sort(unique(weekdays(.leap.seconds))), each=24), 
                       hour=rep(1:24, 7), count=sample(2:600, 24*7, replace=T))

df$weekday <- factor(df$weekday, levels=c("Monday", "Tuesday", "Wednesday", 
                      "Thursday", "Friday", "Saturday", "Sunday"), ordered=T)
df$hour <- factor(df$hour)

require(ggplot2)    
p <- ggplot(data = df, aes(x=hour)) 
p <- p + geom_histogram(aes(weights=count, fill=weekday))
p <- p + scale_fill_brewer(palette="Set3")
p <- p + facet_wrap( ~ weekday, ncol=1)
p

ggplot2_facet_histogram

like image 168
Arun Avatar answered Oct 06 '22 11:10

Arun