ggplot bar chart for time series


Solution 1

Hope this is what you are looking for:

ggplot2 requires data in a long format.

m_molten <- melt(m, "dates.f")

Plotting itself is done by

ggplot(m_molten, aes(x=dates.f, y=value, fill=variable)) + 

enter image description here

You can add position="dodge" to geom_bar if you want then side by side.


If you want yearly breaks only: convert m_molten$dates.f to date.

m_molten$dates.f <- as.Date(m_molten$dates.f)

ggplot(m_molten, aes(x=dates.f, y=value, fill=variable)) + 
  geom_bar(stat="identity") + 
  scale_x_date(labels = date_format("%y"), breaks = date_breaks("year"))

P.S.: See for Hadley's philosophy of tidy data.

Solution 2

To create the plot you need, you have to reshape your data from "wide" to "tall". There are many ways of doing this, including the reshape() function in base R (not recommended), reshape2 and tidyr.

In the tidyr package you have two functions to reshape data, gather() and spread().

The function gather() transforms from wide to tall. In this case, you have to gather your columns V1:V5.

Try this:


tidy_m <- gather(m, var, value, V1:V5)

ggplot(tidy_m,aes(x = dates.f, y=value, fill=var)) +

enter image description here


Related videos on Youtube

Author by


Updated on June 04, 2022


  • math
    math over 1 year

    I'm reading the book by Hadley Wickham about ggplot, but I have trouble to plot certain weights over time in a bar chart. Here is sample data:

    dates <- c("20040101","20050101","20060101")
    dates.f <- strptime(dates,format="%Y%m%d")
    m <- rbind(c(0.2,0.5,0.15,0.1,0.05),c(0.5,0.1,0.1,0.2,0.1),c(0.2,0.2,0.2,0.2,0.2))
    m <- cbind(dates.f,

    This data.frame has in the first column the dates and each row the corresponding weights. I would like to plot the weights for each year in a bar chart using the "fill" argument.

    I'm able to plot the weights as bars using:

    p <- ggplot(m,aes(dates.f))

    However, this is not exactly what I want. I would like to see in each bar the contribution of each weight. Moreover, I don't understand why I have the strange format on the x-axis, i.e. why there is "2004-07" and "2005-07" displayed.

    Thanks for the help

  • math
    math over 8 years
    maybe one small question: I wanted to change the labels via scale_x_date(lables=date_format("%y")). This is gives an error that it cant find the function date_format. How can I just display 04, 05,06?