Skip to content

Changing X axis of ggplot

6 messages · Aren Cambre, Hasan Diwan, Joshua Wiley

#
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:
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)
#
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:

  
    
#
It's below. And I meant offense_time, not offense_hour.
structure(list(offense_time = structure(c(0.389583333333333,
0.336111111111111, 0.427083333333333, 0.409722222222222, 0.4375,
0.648611111111111, 0.471527777777778, 0.561111111111111, 0.0111111111111111,
0.679166666666667, 0.572916666666667, 0.847222222222222, 0.548611111111111,
0.538194444444444, 0.702083333333333, 0.03125, 0.895833333333333,
0.826388888888889, 0.5, 0.114583333333333, 0.755555555555556,
0.84375, 0.278472222222222, 0.631944444444444, 0.545138888888889,
0.596527777777778, 0.642361111111111, 0.540277777777778, 0.71875,
0.861111111111111, 0.673611111111111, 0.381944444444444, 0.465277777777778,
0.697916666666667, 0.645833333333333, 0.916666666666667, 0.732638888888889,
0.611111111111111, 0.586805555555556, 0.59375), format = "h:m:s",
class = "times")), .Names = "offense_time", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40"), class = "data.frame")
On Tue, Jan 3, 2012 at 12:13 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
#
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:
#
Hi Aren,

Thanks for sending the data.  I poked around a bit, and here was what
I came up with (a bit of a hack really, but perhaps acceptable
enough).  lbl_formatter looks like another way to go.

require(ggplot2)
require(chron)

## hack to deal with non exported method
parse.format <- chron:::parse.format

## now just use chron's format.times() function as your formatter:
ggplot(dallas, aes(x = offense_time)) +
  geom_bar() +
  coord_polar() +
  scale_x_continuous(formatter = chron:::format.times)

Somewhat sensibly, the ~22-06 hours are relatively light (at least in
the little subset of your data I looked at).

Note that the ::: is used because I am using functions in chron it
never meant for end users to use directly.

Happy graphing!

Josh
On Tue, Jan 3, 2012 at 6:57 PM, Aren Cambre <aren at arencambre.com> wrote: