Tuesday, July 2, 2013

Stacked graphs “unmelted” data with ggplot2


I recently had a problem that took lots of my time trying to figure out the solutions. I had wanted to make a stacked bar chart in ggplot2. I know how to make one with barplot(), but I wanted to use ggplot2 because it's very easy to make the bars and use melted data. But what about if your data is not melted, like my data below ?
Dates Registrations.x Registrations.y
1   2013-01-01               7              21
2   2013-01-02              15              36
3   2013-01-03              13              23
4   2013-01-04              20              16
5   2013-01-05              14              28
6   2013-01-06              17              22


df <- data.frame(
 Registrations.x = c(7,15,13,20,14,17),
 Registrations.y = c(21,36,23,16,28,22))
also add:-
> df$Dates <- c("2013-01-01", "2013-01-02", "2013-01-03", "2013-01-04", "2013-01-05", "2013-01-06")
What I want is a plot with categories where the Dates are on the X axis, and for each of those, the values for different registration stats  stacked on top of each other on the Y axis. Most graphs and examples with R that I have seen plot only one variable on the Y axis, so this how to go about with a different way using ggplot.
First, we need to do some data manipulation by adding a different category as a variable and melting the data to long format. WTF is melting data?


row.names(df) <- df$Dates
mdf <- melt(df, id.vars = "Dates")
fyi - incase you get errors like “Error: could not find function "melt"” make sure that you got the “reshape” package installed and to do that run the command below.
>install.packages("reshape")
>library(reshape)
for more details you find out more with ?melt() on R prompt.


Now we can plot the stacked bar, using the variable named “Dates” to determine the fill colour of each bar.
g <- ggplot(mdf, aes(Dates, y=value, fill = variable)) + geom_bar(stat="identity")

No comments:

Post a Comment

Add any comments if it helped :)