Re: Histogram - Months in Chronological Order

2,307 views
Skip to first unread message

Ben Bond-Lamberty

unread,
Aug 8, 2012, 12:09:40 PM8/8/12
to Alex, ggp...@googlegroups.com
Alex,

A reproducible example would be useful. But:

1) A clustered bar chart can be done using geom_bar( position="dodge"
). See ?geom_bar. See ?coord_polar for an example of making a pie
chart; I'm not clear what a clustered pie chart is.

2) Probably what you'd like to do here is to use R's strptime function
to convert your x-axis data to calendar dates. Then ggplot will
display things in the correct order. Alternatively, if you want to
keep those labels as factors, reorder them before plotting. Google
"reorder factor levels in r" for many examples of this.

Hope this is useful-
Ben


On Wed, Aug 8, 2012 at 10:37 AM, Alex <anoop....@gmail.com> wrote:
> Hello All,
>
> I started using R a weeks ago and I am loving this tool. I have an issue
> while I am trying to plot a histogram. The code I am using to plot is given
> below and the graph generated is attached.
>
> Code
> Line 1 p5 <- ggplot(plot2, aes(x=Months)) +
> geom_histogram(aes(y=Measured)) + ylab("Measured Energy Usage, (kWh)")
> Line -2 p5 + opts(axis.text.x = theme_text(angle=90))
>
> Question
> 1) Can ggplot plot clustered bar chart and pie chart like in excel. if
> yes(which I am pretty sure would be) where can i find a detailed step by
> step instruction
> 2)Please see the attached graph and as you notice my months are in
> alphabetical order. How can i change that to chronological order.
>
> I do apologize if this questions are asked before. I did a research online
> before coming here to find a solution and was unsuccessful.
>
> Please guide. Any help is much appreciated.
>
> Regards,
> Alex
>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Alex

unread,
Aug 9, 2012, 1:45:01 PM8/9/12
to ggp...@googlegroups.com
Hello All,

I am not able to fix it. Does my date have to be in any particular format? Currently the columns "Months", "Year" & "Month" come form a .csv file in the format you see in the data frame(plot2)
I am attaching the data frame and my code. Please also see the graph "Rplot2" attached. You will see the issue when you see the graph.

Plot2(data frame)
    Months Year     Month AvgT Predicted Measured   Residuals          SR
1  11/01/2007 2007  November 39.6  8021.491     8712   690.50905  1.47413157
2  12/01/2007 2007  December 28.0  8867.354     8529  -338.35405 -0.72233433
3  01/01/2008 2008   January 23.7  9180.907     8990  -190.90675 -0.40755682
4  02/01/2008 2008  February 23.2  9217.366     9833   615.63363  1.31428397
5  03/01/2008 2008     March 35.2  8342.336     8701   358.66442  0.76569388
6  04/01/2008 2008     April 49.6  7292.299     6798  -494.29863 -1.05525223
7  05/01/2008 2008       May 55.8  6306.289     6388    81.71067  0.17443983
8  06/01/2008 2008      June 71.1  6539.195     6599    59.80526  0.12767511
9  07/01/2008 2008      July 74.3  6539.992     6620    80.00812  0.17080514
10 08/01/2008 2008    August 72.9  6539.643     6660   120.35687  0.25694357
11 09/01/2008 2008 September 66.3  6537.999     6659   121.00097  0.25831862
12 10/01/2008 2008   October 52.9  6599.885     6537   -62.88523 -0.13425039
13 11/01/2008 2008  November 39.5  8028.783     6960 -1068.78288 -2.28168854
14 12/01/2008 2008  December 23.5  9195.491     8545  -650.49060 -1.38869828
15 01/01/2009 2009   January 17.0  9669.466     9699    29.53439  0.06305142
16 02/01/2009 2009  February 28.8  8809.019     9076   266.98134  0.56996446
17 03/01/2009 2009     March 39.9  7999.615     8403   403.38482  0.86116510
18 04/01/2009 2009     April 48.3  7387.094     7851   463.90637  0.99036939
19 05/01/2009 2009       May 55.9  6302.758     6274   -28.75837 -0.06139474
20 06/01/2009 2009      June 67.8  6538.373     6010  -528.37269 -1.12799516
21 07/01/2009 2009      July 68.9  6538.647     6610    71.35329  0.15232840

Code
  mdata1 <- melt(plot2, id=c("AvgT", "Residuals", "SR", "Months", "Year", "Month"))
  sp <- ggplot(mdata1, aes(x=Month, y=value)) + geom_histogram(binwidth=2) +ylab("Energy Usage, (kWh)") +opts(axis.text.x = theme_text(angle=90))
  sp +facet_grid(Year~ variable)

Please let me know what I am doing wrong. I would really appreciate a fixing code.

Regards,
Alex

Please see the attached data frame
Rplot02.jpeg

Roman Luštrik

unread,
Aug 9, 2012, 1:55:10 PM8/9/12
to Alex, ggp...@googlegroups.com
Your Months variable (factor) is ordered lexicographically. You should reorder it to reflect the actual month order. See ?factor or ?levels.

Cheers,
Roman



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2



--
In God we trust, all others bring data.

Alex

unread,
Aug 9, 2012, 2:38:59 PM8/9/12
to Roman Luštrik, ggp...@googlegroups.com
Hello Roman,
Thank you very much, it worked.

I am just attaching the code, so that it cal be useful for everyone

Code (in bold is the new addition)

  mdata1 <- melt(plot2, id=c("AvgT", "Residuals", "SR", "Months", "Year", "Month"))
  mdata1$Month = factor(mdata1$Month,levels=c("January","February","March", "April","May","June","July","August","September", "October","November","December"),ordered=TRUE)
  Month[1] < Month [2]
  table(Month)
  mdata1

  sp <- ggplot(mdata1, aes(x=Month, y=value)) + geom_histogram(binwidth=2) +ylab("Energy Usage, (kWh)") +opts(axis.text.x = theme_text(angle=90))
  sp +facet_grid(Year~ variable)

Cheers
Alex
--
"I cried because I had no shoes till i saw a man with no legs"
Reply all
Reply to author
Forward
0 new messages