Thanks to Joshua Wiley for turning me on to ggplot2. I am making a plot using this: p <- ggplot(dallas, aes(x = offense_hour)) + geom_bar() + coord_polar() Dallas is a data frame, and offense_hour is a column with chron objects from the chron library. In this case, the chron object was created with the times function. It is only a time (H:M:S) with no date attached. The plot shows up fine, but the X axis labels are 0.0 through 1.0. How do I convert this to 0:00 through 23:59 (or whatever may be appropriate given the breaks)? My searches lead me to scale_x_discrete, but I am not clear if that's even the right function. Aren
Aren, On 2 January 2012 19:34, Aren Cambre <aren at arencambre.com> wrote:> I am making a plot using this: > p <- ggplot(dallas, aes(x = offense_hour)) + geom_bar() + coord_polar() > The plot shows up fine, but the X axis labels are 0.0 through 1.0. How > do I convert this to 0:00 through 23:59 (or whatever may be > appropriate given the breaks)?Seeing as there is no source code, I'm taking a stab in the dark, but the below gives me a pie chart: to <- as.POSIXlt('23:59:59', format='%H:%M:%S', origin=Sys.Date()) from <- as.POSIXlt('00:00:00', format='%H:%M:%S', origin=Sys.Date()) range <- seq(from, to, by = .5) dallas <- data.frame(offense_hour = range, stuff = rnorm(c(1:length(range)))) p <- ggplot(dallas, aes(x = offense_hour) + geom_bar() + coord_polar) -- Sent from my mobile device Envoyait de mon portable
Hi Aren, Could you perhaps send us the output of: dput(dallas[1:40, "offense_hour", drop = FALSE]) I believe your problem, but getting it to work in ggplot2 will be easiest working with your actual data (or a bit of it anyway). for ggplot2 specific questions, you might also checkout: groups.google.com/group/ggplot2 a lot of very clever ggplot2ers there (including the author much more than he is around Rhelp). Cheers, Josh On Mon, Jan 2, 2012 at 7:34 PM, Aren Cambre <aren at arencambre.com> wrote:> Thanks to Joshua Wiley for turning me on to ggplot2. > > I am making a plot using this: > p <- ggplot(dallas, aes(x = offense_hour)) + geom_bar() + coord_polar() > > Dallas is a data frame, and offense_hour is a column with chron > objects from the chron library. In this case, the chron object was > created with the times function. It is only a time (H:M:S) with no > date attached. > > The plot shows up fine, but the X axis labels are 0.0 through 1.0. How > do I convert this to 0:00 through 23:59 (or whatever may be > appropriate given the breaks)? > > My searches lead me to scale_x_discrete, but I am not clear if that's > even the right function. > > Aren > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
I'm not sure I know yet what I'm looking for. :-) I have a list of all traffic tickets written in Dallas, TX over a few years. One of the first things I am doing is reviewing the data to make sure the quality is good; I'm trying to see whether it just looks right. One measure is whether the volume of tickets written each hour of the day make sense. I've converted each ticket's time into a times class (from the chron package). The problem is that this is expressed as a decimal value between 0 and 1. For example, take 0.538194444444444. That is the underlying value behind a times object. 0.538194444444444 * 24 = 12.916666666666656. OK, so this is 12 noon and some minutes. 0.916666666666656 * 60 = 55. OK, it's 12:55 PM. Now if I put these times in a barplot, I'd like to group by each hour, which would be increments of 1/24 = 0.041667. And I'd like the labels on the X axis to be like 1 AM, 2 AM, 3 AM instead of 0.041667, 0.08333, and 0.125. I'm not clear how to do this with ggplot. Aren On Tue, Jan 3, 2012 at 8:40 AM, Hasan Diwan <hasan.diwan at gmail.com> wrote:> Aren, > Perhaps putting the sort of visualisation you'd like in a > web-accessible location would be helpful? > > -- > Sent from my mobile device > Envoyait de mon portable
Got it figured out. I found this post on the ggplot2 Google Group: http://groups.google.com/group/ggplot2/browse_thread/thread/698e658b6dfec56c/5390824dab4a1cd7 It recommends you make this function: lbl_formatter <- function(x) { h <- floor(x/60) m <- floor(x %% 60) s <- round(60*(x %% 1)) # Round to nearest second sprintf('%02d:%02d:%02d', h, m, s) } Then you assign it as the formatter using scale_x_continuous(formatter = lbl_formatter). Aren On Mon, Jan 2, 2012 at 9:34 PM, Aren Cambre <aren at arencambre.com> wrote:> > Thanks to Joshua Wiley for turning me on to ggplot2. > > I am making a plot using this: > p <- ggplot(dallas, aes(x = offense_hour)) + geom_bar() + coord_polar() > > Dallas is a data frame, and offense_hour is a column with chron > objects from the chron library. In this case, the chron object was > created with the times function. It is only a time (H:M:S) with no > date attached. > > The plot shows up fine, but the X axis labels are 0.0 through 1.0. How > do I convert this to 0:00 through 23:59 (or whatever may be > appropriate given the breaks)? > > My searches lead me to scale_x_discrete, but I am not clear if that's > even the right function. > > Aren